<?php

namespace Macymed\MacymedLinkTracker\Utils;

if (! defined('ABSPATH')) {
    exit; // Sécurité
}

class Assets
{
    public static function init()
    {



        add_action(
            'wp_enqueue_scripts',
            function () {
                wp_enqueue_style('macymed-redirect-style', Utils::get_plugin_url() . 'dist/css/redirect.css' . '?t=' . time(), [], '1.0.0');
                wp_enqueue_script(
                    'chartjs', // Handle du script
                    Utils::get_plugin_url() . 'dist/js/vendor/chart.min.js',
                    [], // Pas de dépendances
                    '4.4.7',
                    false
                );
                wp_enqueue_script(
                    'macymed-link-tracker-js', // Handle du script
                    Utils::get_plugin_url() . ('/dist/js/replace-external-links.js') . '?t=' . time(),
                    ['chartjs', 'jquery'], // Dépendances (vide ici)
                    '1.0.1', // Version du script
                    true // Charge le script dans le footer pour des performances optimales
                );
                wp_localize_script('macymed-link-tracker-js', 'macymedLinkTrackerScriptAjax', [
                    'nonce' => wp_create_nonce('redirect_external_action'),
                    'record_nonce' => wp_create_nonce('link_tracker_action'),
                    'url_record' => 'https://' . TrackerHelper::get_http_host() . "/link_tracker_record.php?macymed_tracker_record=1&redirect_type=js&mtl_nonce=" . wp_create_nonce('link_tracker_record_action'),
                    'macymedTrackerConfig' => wp_json_encode(self::getConfigForJson(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
                ]);
            }
        );
    }

    public static function getConfigForJson()
    {
        $macymedExclusionRelativePath = intval(get_option('macymed_tracker_exclusion_domains_relative_path', 1));
        $exclusionsUserAgent                             = get_option('macymed_tracker_exclusion_user_agents', []);
        if (! is_array($exclusionsUserAgent)) {
            $exclusionsUserAgent = explode(PHP_EOL, $exclusionsUserAgent);
        }

        $exclusionsParamsGet = get_option('macymed_tracker_exclusion_get', []);
        if (! is_array($exclusionsParamsGet)) {
            $exclusionsParamsGet = explode(PHP_EOL, $exclusionsParamsGet);
        }

        $exclusionsUrl = []; // get_option('macymed_tracker_exclusion_url', []);
        if (! is_array($exclusionsUrl)) {
            $exclusionsUrl = explode(PHP_EOL, $exclusionsUrl);
        }

        $exclusionsDomains = get_option('macymed_tracker_exclusion_domains', []);
        if (! is_array($exclusionsDomains)) {
            $exclusionsDomains = explode(PHP_EOL, $exclusionsDomains);
        }

        $exclusionsProtocols = get_option('macymed_tracker_exclusion_protocols', []);
        if (! is_array($exclusionsProtocols)) {
            $exclusionsProtocols = explode(PHP_EOL, $exclusionsProtocols);
        }

        // Encoder les données en JSON pour JavaScript
        $config = [
            'relativePath' => $macymedExclusionRelativePath,
            'userAgents'   => $exclusionsUserAgent,
            'paramsGet'    => $exclusionsParamsGet,
            'urls'         => $exclusionsUrl,
            'domains'      => $exclusionsDomains,
            'protocols'    => $exclusionsProtocols,
        ];
        return ($config);
    }
}
