<?php

namespace PWH_DCFH\App\Base;

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

        private $min = '';

        private static $localizations = [];

        /**
         * 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('wp_enqueue_scripts', [$this, 'maybe_enqueue_scripts'], 10);
            add_filter('script_loader_tag', [$this, 'maybe_filter_script_tag'], 10, 3);
            add_action('wp_print_scripts', [$this, 'maybe_remove_third_party_localized_script']);
        }

        /**
         * Enqueue Scripts
         */
        public function maybe_enqueue_scripts()
        {
            // Localizations
            self::$localizations = [
                'ajaxURL' => esc_js(admin_url('admin-ajax.php')),
                'ajaxNonce' => wp_create_nonce('pwh-dcfh-nonce'),
                'pluginURL' => esc_js(pwh_dcfh_wp_helpers()::plugin_url()),
                'blogURL' => get_bloginfo('url'),
                'wpLocale' => pwh_dcfh_wp_helpers()::get_wp_locale(),
                'wpMaxUploadSize' => wp_max_upload_size(),
                'wpMaxUploadSizeFormatted' => size_format(wp_max_upload_size()),
                'imageMimeTypes' => pwh_dcfh_wp_helpers()::get_image_mimes(),
                'isSuperAdmin' => is_super_admin(),
                'userIPAddress' => md5(pwh_dcfh_helpers()::get_ip_address()),
                'isPopupsforDivi' => is_plugin_active('popups-for-divi/plugin.php'),
                'i10n' => PWH_DCFH_Strings::instance()->strings('frontend_i10'),
            ];
            /*---------------- App ----------------*/
            wp_enqueue_style('divi-contact-form-helper', pwh_dcfh_wp_helpers()::plugin_url("assets/public/css/app$this->min.css"), [], $this->version);
            wp_enqueue_script('divi-contact-form-helper', pwh_dcfh_wp_helpers()::plugin_url("assets/public/js/app$this->min.js"), ['jquery', 'wp-i18n'], $this->version, true);
            wp_localize_script('divi-contact-form-helper', 'pwh_dcfh_params', apply_filters('pwh_dcfh_f_public_localizations', self::$localizations));
            //wp_set_script_translations('divi-contact-form-helper', 'pwh-dcfh');
            /*---------------- DateTime Picker ----------------*/
            wp_register_style('divi-contact-form-helper-datetimepicker', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.min.css', [], '2.5.20');
            wp_register_script('divi-contact-form-helper-datetimepicker', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js', ['jquery'], '2.5.20', true);
            /*---------------- Select2 ----------------*/
            wp_register_style('divi-contact-form-helper-select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css', [], '4.1.0');
            wp_register_script('divi-contact-form-helper-select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', ['jquery'], '4.1.0', true);
            /*---------------- Digital Signature Pad ----------------*/
            wp_register_script('divi-contact-form-helper-signature-pad', 'https://cdn.jsdelivr.net/npm/signature_pad@5.0.1/dist/signature_pad.umd.min.js', ['jquery'], '5.0.1', true);
            /*---------------- Google reCAPTCHA V2 ----------------*/
            wp_register_script('divi-contact-form-helper-google-recaptcha-v2', 'https://www.google.com/recaptcha/api.js?onload=onloadGoogleRecaptchaCallback&render=explicit', ['jquery'], null, true);
            /*---------------- Cloudflare Turnstile ----------------*/
            wp_register_script('divi-contact-form-helper-cloudflare-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback', ['jquery'], null, true);
            /*---------------- Input Mask ----------------*/
            wp_register_script('divi-contact-form-helper-inputmask', 'https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/5.0.6/jquery.inputmask.min.js"', null, '5.0.6', true);
        }

        /**
         * Filter Script Tag
         *
         * @param $tag
         * @param $handle
         * @param $src
         *
         * @return array|mixed|string|string[]
         */
        public function maybe_filter_script_tag($tag, $handle, $src)
        {
            if ('divi-contact-form-helper-google-recaptcha-v2' === $handle) {
                return str_replace(' src', ' async defer src', $tag);
            }

            return $tag;
        }

        /**
         * Completely Disable Google ReCAPTCHA
         * @return void
         */
        public function maybe_remove_third_party_localized_script()
        {
            if (pwh_dcfh_db()::is_setting_enabled('pwh_dcfh_disable_google_recaptcha')) {
                global $wp_scripts;
                if (isset($wp_scripts->registered['et-recaptcha-v3'])) {
                    $wp_scripts->registered['et-recaptcha-v3']->src = '';
                }
                if (isset($wp_scripts->registered['et-core-api-spam-recaptcha'])) {
                    $wp_scripts->registered['et-core-api-spam-recaptcha']->src = '';
                    $wp_scripts->registered['et-core-api-spam-recaptcha']->extra['data'] = '';
                }
            }
        }
    }
}