<?php
class kavehElementor
{

    public function __construct()
    {
        if (did_action('elementor/loaded')) {
            add_action('elementor/editor/before_enqueue_scripts', [$this ,  'customizeWidgetIcons']);
            add_action('elementor/editor/before_enqueue_scripts', [$this ,  'enqueueStyles']);
            // Handle AJAX request for product attribute terms
            add_action('wp_ajax_kaveh_get_product_attribute_terms', [$this , 'kaveh_get_product_attribute_terms']);
            add_action('wp_ajax_nopriv_kaveh_get_product_attribute_terms', [$this , 'kaveh_get_product_attribute_terms']);
            // Handle AJAX request for taxonomy terms
            add_action('wp_ajax_kaveh_get_taxonomy_terms', [$this , 'kaveh_get_taxonomy_terms']);
            add_action('wp_ajax_nopriv_kaveh_get_taxonomy_terms', [$this , 'kaveh_get_taxonomy_terms']);
            add_action('elementor/editor/before_enqueue_scripts', [$this , 'kaveh_elementor_enqueue_editor_scripts']);
        }
    }

    



    public function enqueueStyles()
    {
        wp_register_style('kaveh-elementor-editor-style', KAVEH_TEMPLATE_URI . '/assets/css/kaveh-elementor-editor-style.css', array(), '1.0.0');
        wp_enqueue_style('kaveh-elementor-editor-style');
    }


    public function customizeWidgetIcons()
    {
        if (get_locale() == 'fa_IR' || get_locale() == 'fa_AF') {
            $logo_url = KAVEH_TEMPLATE_URI . '/assets/images/kaveh/logo-el.png';
            $css = '.elementor-element .icon .eicon-archive-posts:before {
                color: transparent;
                background-image: url(' . $logo_url . ');
                display: block;
                background-size: contain;
                background-repeat: no-repeat;
                background-position: center center;
                height: 100%;
                width: 80px;
            }';
            wp_add_inline_style('kaveh-elementor-editor-style', $css);
        }
    }

    public function kaveh_get_taxonomy_terms()
    {
        if (!isset($_POST['taxonomy'])) {
            wp_send_json_error('No taxonomy provided');
        }

        // Ensure the taxonomy parameter is an array
        $taxonomies = (array) $_POST['taxonomy'];
        $all_terms = [];

        foreach ($taxonomies as $taxonomy) {
            $taxonomy = sanitize_text_field($taxonomy);
            $terms = get_terms([
                'taxonomy' => $taxonomy,
                'hide_empty' => false,
            ]);

            if (is_wp_error($terms)) {
                wp_send_json_error($terms->get_error_message());
            }

            // Merge terms from all taxonomies
            $all_terms = array_merge($all_terms, $terms);
        }


        wp_send_json_success(['terms' => $all_terms]);
    }

    public function kaveh_get_product_attribute_terms()
    {
        if (!isset($_POST['attribute'])) {
            wp_send_json_error('No attribute provided');
        }

        // Ensure the attribute parameter is an array
        $attributes = (array) $_POST['attribute'];
        $all_terms = [];

        foreach ($attributes as $attribute) {
            $attribute = sanitize_text_field($attribute);
            $taxonomy = 'pa_' . $attribute; // WooCommerce product attribute taxonomy prefix

            // Fetch terms for the attribute taxonomy
            $terms = get_terms([
                'taxonomy' => $taxonomy,
                'hide_empty' => false,
            ]);

            if (is_wp_error($terms)) {
                wp_send_json_error($terms->get_error_message());
            }

            // Merge terms from all attributes
            $all_terms = array_merge($all_terms, $terms);
        }

    
        wp_send_json_success(['terms' => $all_terms]);
    }

    public function kaveh_elementor_enqueue_editor_scripts()
    {
        // Enqueue the external JavaScript file
        wp_enqueue_script(
            'kaveh-elementor-editor-script', // Handle
            KAVEH_TEMPLATE_URI . '/assets/js/kaveh-elementor-editor.js', // Path to the external JS file
            ['jquery'], // Dependencies
            '1.0', // Version
            true // Load in footer
        );
        // Localize the script with the AJAX URL
        wp_localize_script('kaveh-elementor-editor-script', 'kaveh_editor_ajax', [
            'ajax_url' => admin_url('admin-ajax.php'),
        ]);
    }


}