(function ($) {
    "use strict";

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

        const settings = JSON.parse(wrapper.getAttribute('data-unfold'));
        const toggleBtn = wrapper.querySelector('.sf-unfold-btn');
        const textNode = toggleBtn.querySelector('.sf-btn-text');
        const iconExpand = toggleBtn.querySelector('.icon-expand');
        const iconCollapse = toggleBtn.querySelector('.icon-collapse');

        let contentWrap, fade;
        let isExpanded = false;
        
        const collapsedHeight = parseInt(getComputedStyle(wrapper).getPropertyValue('--sf-collapsed-height')) || 150;

        if (settings.source === 'container' || settings.source === 'css-class-or-id') {
            if (!settings.target) return;
            contentWrap = document.querySelector(settings.target);
            if (!contentWrap) {
                console.warn('Sifency Unfold: Target not found ->', settings.target);
                return;
            }
            
            contentWrap.style.overflow = 'hidden';
            contentWrap.style.position = 'relative';
            contentWrap.classList.add('sifency-external-unfold');
            
            if (settings.enableFade) {
                fade = contentWrap.querySelector('.sifency-unfold-fade');
                if (!fade) {
                    fade = document.createElement('div');
                    fade.className = 'sifency-unfold-fade';
                    fade.style.background = `linear-gradient(to bottom, transparent, ${settings.fadeColor})`;
                    contentWrap.appendChild(fade);
                }
            }
        } else {
            contentWrap = wrapper.querySelector('.sifency-unfold-content-wrap');
            fade = wrapper.querySelector('.sifency-unfold-fade');
        }

        contentWrap.style.transition = `max-height ${settings.duration}ms ease-in-out`;

        const checkHeight = () => {
            if (contentWrap.scrollHeight <= collapsedHeight) {
                toggleBtn.style.display = 'none';
                if (fade) fade.style.display = 'none';
                contentWrap.style.maxHeight = 'none';
            } else {
                contentWrap.style.maxHeight = `${collapsedHeight}px`;
            }
        };

        setTimeout(checkHeight, 300);
        $(window).on('resize', checkHeight);

        toggleBtn.addEventListener('click', (e) => {
            e.preventDefault();
            isExpanded = !isExpanded;

            if (isExpanded) {
                contentWrap.style.maxHeight = `${contentWrap.scrollHeight}px`;
                wrapper.classList.add('is-unfolded');
                if (fade) fade.style.opacity = '0';

                textNode.innerText = settings.txtCollapse;
                if(iconExpand) iconExpand.style.display = 'none';
                if(iconCollapse) iconCollapse.style.display = 'inline-flex';

                if (settings.scrollTop) {
                    setTimeout(() => { wrapper.scrollIntoView({ behavior: 'smooth', block: 'start' }); }, settings.duration);
                }

                setTimeout(() => { if(isExpanded) contentWrap.style.maxHeight = 'none'; }, settings.duration);
            } else {
                contentWrap.style.maxHeight = `${contentWrap.scrollHeight}px`;
                
                void contentWrap.offsetWidth; 
                
                contentWrap.style.maxHeight = `${collapsedHeight}px`;
                wrapper.classList.remove('is-unfolded');
                if (fade) fade.style.opacity = '1';

                textNode.innerText = settings.txtExpand;
                if(iconExpand) iconExpand.style.display = 'inline-flex';
                if(iconCollapse) iconCollapse.style.display = 'none';
            }
        });
    };

    $(window).on('elementor/frontend/init', () => {
        elementorFrontend.hooks.addAction('frontend/element_ready/sifency-unfold.default', initSifencyUnfold);
    });
})(jQuery);