jQuery(document).ready(function ($) {
    console.log("External script loaded in Elementor editor"); // Debugging

    // Delegated event listener for taxonomy filter
    $(document).on("change", ".elementor-control-taxonomy_filter_productscarouselkaveh select", function () {
        console.log("Taxonomy filter changed"); // Debugging
        var selectedTaxonomy = $(this).val(); // This will be an array if multiple selections are allowed
        var widgetID = $(this).closest(".elementor-control").data("id");

        // Send an AJAX request to fetch terms for the selected taxonomy
        $.ajax({
            url: kaveh_editor_ajax.ajax_url, // Use localized AJAX URL
            type: "POST",
            data: {
                action: "kaveh_get_taxonomy_terms",
                taxonomy: selectedTaxonomy, // This will be an array
            },
            success: function (response) {
                console.log("AJAX Response:", response); // Debugging
                if (response.success) {
                    var terms = response.data.terms;
                    var options = "";

                    // Populate the terms control
                    $.each(terms, function (key, term) {
                        options += "<option value=\"" + term.term_id + "\">" + term.name + "</option>";
                    });

                    // Find the terms control and update its options
                    $(".elementor-control-taxonomy_terms_productscarouselkaveh select").html(options);
                } else {
                    console.error("AJAX Error:", response.data); // Debugging
                }
            },
            error: function (xhr, status, error) {
                console.error("AJAX Request Failed:", status, error); // Debugging
            }
        });
    });

    // Delegated event listener for product attributes
    $(document).on("change", ".elementor-control-product_attributes_productscarouselkaveh select", function () {
        console.log("Product attribute filter changed"); // Debugging
        var selectedAttribute = $(this).val(); // This will be an array if multiple selections are allowed
        var widgetID = $(this).closest(".elementor-control").data("id");

        // Send an AJAX request to fetch terms for the selected attribute
        $.ajax({
            url: kaveh_editor_ajax.ajax_url, // Use localized AJAX URL
            type: "POST",
            data: {
                action: "kaveh_get_product_attribute_terms",
                attribute: selectedAttribute, // This will be an array
            },
            success: function (response) {
                console.log("AJAX Response:", response); // Debugging
                if (response.success) {
                    var terms = response.data.terms;
                    var options = "";

                    // Populate the terms control
                    $.each(terms, function (key, term) {
                        options += "<option value=\"" + term.term_id + "\">" + term.name + "</option>";
                    });

                    // Find the terms control and update its options
                    $(".elementor-control-product_attribute_terms_productscarouselkaveh select").html(options);
                } else {
                    console.error("AJAX Error:", response.data); // Debugging
                }
            },
            error: function (xhr, status, error) {
                console.error("AJAX Request Failed:", status, error); // Debugging
            }
        });
    });
});