<?php

/**
 * Contains functions to set up the shared code.
 *
 * @package northmill-online-shared
 * @noinspection PhpUnused Functions to be called by plugins loading this composer package.
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Hub_Account_Setup;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\General_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\Hook_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Ajax_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Assets_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Cron_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Debug_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Globals_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\i18n_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Info\Abstract_Package_Info_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Logger_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Package_Activation_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Sync_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Template_Loader_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Updaters\Abstract_Updater_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Upgrader_Module;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\WooCommerce_Module;
/**
 * Class Shared
 */
abstract class Abstract_Shared_Base
{
    /**
     * The instance of the shared class.
     *
     * @var Abstract_Shared_Base
     */
    protected static Abstract_Shared_Base $base_instance;
    /**
     * The environment the service is running in.
     *
     * @var string
     */
    public string $env = 'production';
    /**
     * Whether the service is running on Northmill hosting.
     *
     * @var bool
     */
    public bool $nm_hosting = \false;
    /**
     * The package info instance.
     *
     * @var Abstract_Package_Info_Module
     */
    public Abstract_Package_Info_Module $package_info;
    /**
     * The admin instance.
     *
     * @var Admin_Module
     */
    public Admin_Module $admin;
    /**
     * The package activation instance.
     *
     * @var Package_Activation_Module
     */
    public Package_Activation_Module $package_activation;
    /**
     * The ajax instance.
     *
     * @var Ajax_Module
     */
    public Ajax_Module $ajax;
    /**
     * The assets instance.
     *
     * @var Assets_Module
     */
    public Assets_Module $assets;
    /**
     * The debug instance.
     *
     * @var Debug_Module
     */
    public Debug_Module $debug;
    /**
     * The logger instance.
     *
     * @var Logger_Module
     */
    public Logger_Module $logger;
    /**
     * The i18n instance.
     *
     * @var i18n_Module
     */
    public i18n_Module $i18n;
    /**
     * The template loader instance.
     *
     * @var Template_Loader_Module
     */
    public Template_Loader_Module $template_loader;
    /**
     * The sync instance.
     *
     * @var Sync_Module
     */
    public Sync_Module $sync;
    /**
     * Cron module instance.
     *
     * @var Cron_Module
     */
    public Cron_Module $cron;
    /**
     * Globals module instance.
     *
     * @var Globals_Module
     */
    public Globals_Module $globals;
    /**
     * Updater module instance.
     *
     * @var Abstract_Updater_Module
     */
    public Abstract_Updater_Module $updater;
    /**
     * Upgrader module instance.
     *
     * @var Upgrader_Module
     */
    public Upgrader_Module $upgrader;
    /**
     * WooCommerce module instance.
     *
     * @var WooCommerce_Module
     */
    public WooCommerce_Module $woocommerce;
    /**
     * Get shared instance.
     */
    final public static function base_instance(): Abstract_Shared_Base
    {
        return self::$base_instance;
    }
    /**
     * Shared constructor, it should be run ASAP.
     */
    public function __construct()
    {
        self::$base_instance = $this;
    }
    /**
     * Have child classes set up their hooks into Shared.
     *
     * @param string $service_name_underscored The service name in underscored format.
     *
     * @return void
     */
    protected function setup_hooks(string $service_name_underscored): void
    {
    }
    /**
     * Get the absolute path to the theme style.css, or plugin base PHP file.
     *
     * @return string
     */
    abstract public function get_package_file_path(): string;
    /**
     * Run at the end of child constructor.
     */
    final protected function post_construct(): void
    {
        $this->nm_hosting = General_Helpers::get_constant('NM_HOSTING', \false);
        $this->i18n = new i18n_Module($this);
        $this->package_activation = new Package_Activation_Module($this);
        // Has custom constructor.
        $this->globals = new Globals_Module($this);
        $this->logger = new Logger_Module($this);
        $this->env = wp_get_environment_type();
        $this->admin = new Admin_Module($this);
        $this->debug = new Debug_Module($this);
        // Has custom constructor.
        $this->sync = new Sync_Module($this);
        $this->assets = new Assets_Module($this);
        $this->template_loader = new Template_Loader_Module($this);
        $this->ajax = new Ajax_Module($this);
        $this->cron = new Cron_Module($this);
        $this->upgrader = new Upgrader_Module($this);
        Hook_Helpers::do_scoped_action('northmill_online_shared_post_construct');
    }
    /**
     * Should ALWAYS be run on the 'plugins_loaded' or 'after_setup_theme' action.
     *
     * @return bool True if requirements are met. For instance, WooCommerce is active if required.
     */
    final public function setup(): bool
    {
        $this->woocommerce = new WooCommerce_Module($this);
        if (!$this->woocommerce->is_woocommerce_status_valid()) {
            Hook_Helpers::do_scoped_action('northmill_online_shared_failed_setup');
            return \false;
        }
        $this->internal_setup();
        $this->i18n->setup();
        $this->admin->setup();
        // This sets up settings fields. Also, needed before cron setup.
        $this->logger->setup();
        $this->debug->setup();
        $this->cron->setup();
        // Needed before sync.
        $this->sync->setup();
        $this->assets->setup();
        $this->template_loader->setup();
        $this->ajax->setup();
        $this->upgrader->setup();
        if ($this->updater->should_setup()) {
            $this->updater->setup();
        }
        if (Hub_Account_Setup::is_setting_up_hub_accounts()) {
            Hub_Account_Setup::setup();
        }
        Hook_Helpers::do_scoped_action('northmill_online_shared_after_setup');
        return \true;
    }
    /**
     * Internal setup function that will be run on the 'plugins_loaded' or 'after_setup_theme' action.
     */
    abstract protected function internal_setup(): void;
    /**
     * Get translation data.
     *
     * @return array See format in doc for `Abstract_Translation_Model` constructor`.
     */
    abstract public function get_translations(): array;
}
