(function ($) {
    "use strict";

    const initAccordion = ($scope) => {
        const wrapper = $scope.find('.sifency-accordion-wrapper')[0];
        if (!wrapper) return;

        if (typeof gsap === 'undefined') {
            console.error('Sifency Error: GSAP is not loaded.');
            return;
        }

        const items = wrapper.querySelectorAll('.sifency-acc-item');
        const closeOthers = wrapper.dataset.closeOthers === 'true';

        // Set initial states
        items.forEach((item) => {
            const content = item.querySelector('.sifency-acc-content');
            const iconV = item.querySelector('.sifency-icon-v'); // Vertical line of the '+'

            if (item.classList.contains('sifency-is-active')) {
                // If set to active by default
                gsap.set(content, { height: "auto", opacity: 1 });
                gsap.set(iconV, { rotation: 90, transformOrigin: "center" }); // Turns '+' into '-'
            } else {
                gsap.set(content, { height: 0, opacity: 0 });
                gsap.set(iconV, { rotation: 0, transformOrigin: "center" });
            }
        });

        const toggleAccordion = (clickedItem) => {
            const content = clickedItem.querySelector('.sifency-acc-content');
            const iconV = clickedItem.querySelector('.sifency-icon-v');
            const isActive = clickedItem.classList.contains('sifency-is-active');

            if (closeOthers) {
                items.forEach((item) => {
                    if (item !== clickedItem && item.classList.contains('sifency-is-active')) {
                        item.classList.remove('sifency-is-active');
                        gsap.to(item.querySelector('.sifency-acc-content'), { 
                            height: 0, 
                            opacity: 0, 
                            duration: 0.4, 
                            ease: "power2.inOut" 
                        });
                        gsap.to(item.querySelector('.sifency-icon-v'), { 
                            rotation: 0, 
                            duration: 0.4, 
                            ease: "power2.inOut" 
                        });
                    }
                });
            }

            if (isActive) {
                // Close the clicked item
                clickedItem.classList.remove('sifency-is-active');
                gsap.to(content, { 
                    height: 0, 
                    opacity: 0, 
                    duration: 0.4, 
                    ease: "power2.inOut" 
                });
                gsap.to(iconV, { 
                    rotation: 0, 
                    duration: 0.4, 
                    ease: "power2.inOut" 
                });
            } else {
                // Open the clicked item
                clickedItem.classList.add('sifency-is-active');
                gsap.to(content, { 
                    height: "auto", 
                    opacity: 1, 
                    duration: 0.4, 
                    ease: "power2.inOut" 
                });
                gsap.to(iconV, { 
                    rotation: 90, // Spins to form a '-'
                    duration: 0.4, 
                    ease: "power2.inOut" 
                });
            }
        };

        // Attach Click Events to Headers
        items.forEach((item) => {
            const header = item.querySelector('.sifency-acc-header');
            header.addEventListener('click', () => toggleAccordion(item));
        });
    };

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

})(jQuery);