<?php

namespace PWH_DCFH\App\Base;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Admin_Enqueue')) {
    class PWH_DCFH_Admin_Enqueue
    {
        private $version;

        private $min = '';

        /**
         * Initializer Of The Class
         * Add/Remove Necessary Actions/Filters
         */
        public function init()
        {
            $this->version = PWH_DCFH_PLUGIN_VERSION;
            $this->min = PWH_DCFH_PLUGIN_DEV_MOD ? '' : '.min';
            add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts'], 10, 1);
        }

        /**
         * Enqueue Scripts
         *
         * @param $hook
         *
         * @return void|null
         */
        public function enqueue_scripts($hook)
        {
            /*if (!in_array($hook, [
                'toplevel_page_'.pwh_dcfh_wp_helpers()::get_divi_setting_slug(),
                'pwh_dcfh_page_create_post',
                'pwh_dcfh_page_create_email_template',
                'pwh_dcfh_page_email_templates_list',
                'pwh_dcfh_page_send_email',
                'pwh_dcfh_page_export_as_csv',
                'pwh_dcfh_page_contact_forms',
                'divi_page_et_theme_builder',
                'post-new.php',
                'post.php',
                'edit.php',
                'index.php',
            ], true)) {
                return null;
            }*/
            // Localizations
            $localizations = [
                'ajaxURL' => esc_js(admin_url('admin-ajax.php')),
                'ajaxNonce' => wp_create_nonce('admin-ajax-nonce'),
                'pluginURL' => esc_js(pwh_dcfh_wp_helpers()::plugin_url()),
                'blogURL' => get_bloginfo('url'),
                'i10n' => PWH_DCFH_Strings::instance()->strings('admin_i10'),
                'dataPicker' => ['status' => false]
            ];
            $localizations = apply_filters('pwh_dcfh_f_admin_localizations', $localizations);
            /*---------------- Admin App ----------------*/
            wp_enqueue_style('divi-contact-form-helper-app', pwh_dcfh_wp_helpers()::plugin_url("assets/admin/css/app$this->min.css"), [], $this->version);
            wp_enqueue_script('divi-contact-form-helper-app', pwh_dcfh_wp_helpers()::plugin_url("assets/admin/js/app$this->min.js"), ['jquery', 'wp-i18n'], $this->version, true);
            wp_localize_script('divi-contact-form-helper-app', 'pwh_dcfh_params', $localizations);
            if (!did_action('wp_enqueue_media')) {
                wp_enqueue_media();
            }
            /*---------------- Jquery UI  ----------------*/
            if (!did_action('wp-jquery-ui-dialog')) {
                wp_enqueue_script('jquery-ui-dialog');
                wp_enqueue_style('wp-jquery-ui-dialog');
                wp_enqueue_script('jquery-ui-autocomplete', '', ['jquery-ui-widget', 'jquery-ui-position'], '1.8.6');
            }
            /*---------------- Jquery Datepicker  ----------------*/
            if (!did_action('jquery-ui-datepicker') && (isset($localizations['dataPicker']['status']) && $localizations['dataPicker']['status'])) {
                wp_enqueue_script('jquery-ui-datepicker');
                wp_enqueue_style('jquery-ui-datepicker', 'https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css');
            }
        }
    }
}