(function ($) {
    'use strict';

    /**
     * Sifency Accordion Handler
     *
     * This function is responsible for initializing a single accordion widget.
     * It is triggered by the Elementor Frontend hook.
     *
     * @param {jQuery} $scope The widget's container element as a jQuery object.
     */
    var SifencyAccordionHandler = function ($scope) {
        var $accordion = $scope.find('.sifency-accordion');
        if (!$accordion.length) {
            return;
        }

        var settings = $accordion.data();
        var $questions = $accordion.find('.sifency-accordion-question');
        var speed = settings.toggleSpeed || 300; 
        var canOpenMultiple = settings.multipleOpen === 'yes';

        $accordion.find('.sifency-accordion-item.active .sifency-accordion-answer').show();

        $questions.off('click.sifencyAccordion').on('click.sifencyAccordion', function () {
            var $parentItem = $(this).parent('.sifency-accordion-item');

            if (!canOpenMultiple) {
                $parentItem.siblings('.active')
                    .removeClass('active')
                    .find('.sifency-accordion-answer')
                    .slideUp(speed);
            }
            
            $parentItem.toggleClass('active');
            $parentItem.find('.sifency-accordion-answer').slideToggle(speed);
        });
    };

    $(window).on('elementor/frontend/init', function () {
        elementorFrontend.hooks.addAction(
            'frontend/element_ready/sifency-addons-accordion.default',
            SifencyAccordionHandler
        );
    });

})(jQuery);