<?php
/**
 * ADDON - POP to SdI via PEC
 */

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

// Mode (dev or prod)
if (! defined('WP_FATT_SDI_MODE')) {
    define('WP_FATT_SDI_MODE', 'prod');
}

if (! defined('WP_FATT_SDI_DEV_PEC_TO')) {
    define('WP_FATT_SDI_DEV_PEC_TO', null);
}

// Active Log
if (! defined('WP_FATT_SDI_LOG')) {
    define('WP_FATT_SDI_LOG', false);
}

// Define global plugin constant.
define('WP_FATT_SDI_NAME', 'POP to SdI via PEC');
define('WP_FATT_SDI_PEC_TEXTDOMAIN', 'popsdipec');
define('WP_FATT_SDI_VERSION', '1.0.6');

// Plugin DIR
define('WP_FATT_SDI_PLUGIN_DIR', basename(plugin_dir_path(__FILE__)));
// Dirs
define('WP_FATT_SDI_DIR', plugin_dir_path(__FILE__));
// Uri
define('WP_FATT_SDI_URL', plugin_dir_url(__FILE__));

// Base Requirements.
require_once untrailingslashit(WP_FATT_SDI_DIR . 'src/Plugin.php');
require_once untrailingslashit(WP_FATT_SDI_DIR . 'requires.php');

// Register the activation and deactivation hook.
register_activation_hook(__FILE__, array('WooPoPToSdIPec\\Activate', 'activate'));
register_deactivation_hook(__FILE__, array('WooPoPToSdIPec\\Deactivate', 'deactivate'));

// Init
$bootstrap = function () {

    // Prevent init plugin in other ajax action
    if (! is_admin() && defined('DOING_AJAX') && DOING_AJAX) {
        return;
    }

    if (method_exists(\WooPoPToSdIPec\Plugin::class, 'load_plugin_textdomain')) {
        // Load Localisation files.
        \WooPoPToSdIPec\Plugin::load_plugin_textdomain();
    }

    // Check for the dependency.
    if (\WooPoPToSdIPec\Functions\isWooCommerceActive() &&
        class_exists('WcElectronInvoice\\Plugin')
    ) :
        $filters = array();

        // Global filter
        $filters = array_merge(
            $filters,
            include \WooPoPToSdIPec\Plugin::getPluginDirPath('/inc/filters.php')
        );

        // Admin or Front filter
        if (is_admin()) {
            $filters = array_merge(
                $filters,
                include \WooPoPToSdIPec\Plugin::getPluginDirPath('/inc/filtersAdmin.php')
            );
        } else {
            $filters = array_merge(
                $filters,
                include \WooPoPToSdIPec\Plugin::getPluginDirPath('/inc/filtersFront.php')
            );
        }

        if (\WooPoPToSdIPec\Functions\isAjaxRequest()) {
            $filters = array_merge(
                $filters,
                include \WooPoPToSdIPec\Plugin::getPluginDirPath('/inc/filtersAjax.php')
            );
        }

        // Loader init.
        $init = new WooPoPToSdIPec\Init(new WooPoPToSdIPec\Loader(), $filters);
        $init->init();
    endif;
};

if (did_action('init')) {
    $bootstrap();
} else {
    add_action('init', $bootstrap, 20);
}
