(function ($) {
    "use strict";

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

        if (typeof gsap === 'undefined' || typeof ScrollTrigger === 'undefined') {
            console.error('Sifency: GSAP/ScrollTrigger missing for Scroll Progress.');
            return;
        }

        const type = wrapper.dataset.type;
        const isBTT = wrapper.dataset.btt === 'true';
        const fill = wrapper.querySelector('.sifency-progress-fill');
        const textObj = wrapper.querySelector('.sifency-progress-text');

        if (type === 'circle') {
            const circumference = 2 * Math.PI * 46;
            gsap.set(fill, {
                strokeDasharray: circumference,
                strokeDashoffset: circumference,
                transformOrigin: "center center",
                rotation: -90
            });

            ScrollTrigger.create({
                trigger: document.body,
                start: "top top",
                end: "bottom bottom",
                scrub: 0.1, // Slight smoothness
                onUpdate: (self) => {
                    const offset = circumference - (self.progress * circumference);
                    gsap.set(fill, { strokeDashoffset: offset });

                    // Update Text (0 to 100)
                    if (textObj) {
                        textObj.innerText = Math.round(self.progress * 100) + "%";
                    }
                }
            });

        } else if (type === 'hbar') {
            gsap.set(fill, { transformOrigin: "left center", scaleX: 0 });
            gsap.to(fill, {
                scaleX: 1,
                ease: "none",
                scrollTrigger: {
                    trigger: document.body,
                    start: "top top",
                    end: "bottom bottom",
                    scrub: true,
                }
            });

        } else if (type === 'vbar') {
            gsap.set(fill, { transformOrigin: "center top", scaleY: 0 });
            gsap.to(fill, {
                scaleY: 1,
                ease: "none",
                scrollTrigger: {
                    trigger: document.body,
                    start: "top top",
                    end: "bottom bottom",
                    scrub: true,
                }
            });
        }

        // 2. Back to Top Logic (Lenis Integration)
        if (isBTT) {
            wrapper.addEventListener('click', () => {
                // If Lenis is active (from your first widget), use its smooth scrollTo
                if (window.lenis) {
                    window.lenis.scrollTo(0, { duration: 1.5 });
                } else {
                    // Fallback to native smooth scroll
                    window.scrollTo({ top: 0, behavior: 'smooth' });
                }
            });
        }
    };

    $(window).on('elementor/frontend/init', function () {
        elementorFrontend.hooks.addAction(
            'frontend/element_ready/sifency-scroll-progress.default',
            initScrollProgress
        );
    });

})(jQuery);