module Avo

def self.asset_manager

def self.asset_manager
  @manager ||= AssetManager.new
end

def self.configuration

def self.configuration
  @configuration ||= Configuration.new
end

def self.configuration=(config)

def self.configuration=(config)
  @configuration = config
end

def self.configure

def self.configure
  yield configuration
end

def self.plugin_manager

def self.plugin_manager
  @plugin_manager ||= PluginManager.new
end

def app_status

def app_status
  license.valid?
end

def avo_dynamic_filters_installed?

def avo_dynamic_filters_installed?
  defined?(Avo::DynamicFilters).present?
end

def boot

Runs when the app boots up
def boot
  Turbo::Streams::TagBuilder.prepend(Avo::TurboStreamActionsHelper)
  @logger = Avo.configuration.logger
  @field_manager = Avo::Fields::FieldManager.build
  @cache_store = Avo.configuration.cache_store
  Avo.plugin_manager.reset
  ActiveSupport.run_load_hooks(:avo_boot, self)
  eager_load_actions
end

def check_rails_version_issues

def check_rails_version_issues
  if Rails.version.start_with?("7.1") && Avo.configuration.license.in?(["pro", "advanced"])
    Avo.error_manager.add({
      url: "https://docs.avohq.io/3.0/upgrade.html#upgrade-from-3-7-4-to-3-9-1",
      target: "_blank",
      message: "Due to a Rails 7.1 bug the following features won't work:\n\r
                - Dashboards\n\r
                - Ordering\n\r
                - Dynamic filters\n\r
                We recommend you upgrade to Rails 7.2\n\r
                Click banner for more information."
    })
  end
end

def display_menu_editor_warning

def display_menu_editor_warning
  if Avo.configuration.license == "community" && has_main_menu?
    Avo.error_manager.add({
      url: "https://docs.avohq.io/3.0/menu-editor.html",
      target: "_blank",
      message: "The menu editor is available exclusively with the Pro license or above. Consider upgrading to access this feature."
    })
  end
end

def eager_load_actions

def eager_load_actions
  Rails.autoloaders.main.eager_load_namespace(Avo::Actions) if defined?(Avo::Actions)
end

def extra_gems

def extra_gems
  [:pro, :advanced, :menu, :dynamic_filters, :dashboards, :enterprise, :audits]
end

def has_main_menu?

def has_main_menu?
  return false if Avo.license.lacks_with_trial(:menu_editor)
  return false if Avo.configuration.main_menu.nil?
  true
end

def has_profile_menu?

def has_profile_menu?
  return false if Avo.license.lacks_with_trial(:menu_editor)
  return false if Avo.configuration.profile_menu.nil?
  true
end

def init

Runs on each request
def init
  Avo::Current.error_manager = Avo::ErrorManager.build
  # Check rails version issues only on NON Production environments
  unless Rails.env.production?
    check_rails_version_issues
    display_menu_editor_warning
  end
  Avo::Current.resource_manager = Avo::Resources::ResourceManager.build
  Avo::Current.tool_manager = Avo::Tools::ToolManager.build
  ActiveSupport.run_load_hooks(:avo_init, self)
end

def main_menu

def main_menu
  return unless Avo.plugin_manager.installed?("avo-menu")
  # Return empty menu if the app doesn't have the profile menu configured
  return Avo::Menu::Builder.new.build unless has_main_menu?
  Avo::Menu::Builder.parse_menu(&Avo.configuration.main_menu)
end

def mount_engines

def mount_engines
  -> {
    raise "'mount_engines' method is now obsolete. \n" \
      "Please refer to the upgrade guide for details on the new mounting point: \n" \
      "https://docs.avohq.io/3.0/upgrade.html#Avo's%20mounting%20point%20update"
  }
end

def profile_menu

def profile_menu
  return unless Avo.plugin_manager.installed?("avo-menu")
  # Return empty menu if the app doesn't have the profile menu configured
  return Avo::Menu::Builder.new.build unless has_profile_menu?
  Avo::Menu::Builder.parse_menu(&Avo.configuration.profile_menu)
end

def root_path(paths: [], query: {}, **args)

Generate a dynamic root path using the URIService
def root_path(paths: [], query: {}, **args)
  Avo::Services::URIService.parse(Avo::Current.view_context.avo.root_url.to_s)
    .append_paths(paths)
    .append_query(query)
    .to_s
end