(function ($) {
    'use strict';

    class SifencyScrollManager {
        constructor() {
            this.lenis = null;
            this.rafId = null;
            this.config = {};
            this.init();
        }

        init() {
            if (document.body.classList.contains('elementor-editor-active')) {
                return;
            }

            const configEl = document.getElementById('sifency-lenis-config');
            if (!configEl) return;

            try {
                this.config = JSON.parse(configEl.dataset.lenisConfig);
            } catch (e) {
                console.error('Lenis Config Error:', e);
                return;
            }

            if (typeof Lenis === 'undefined') {
                console.warn('Lenis library is missing.');
                return;
            }

            this.startLenis();
        }

        startLenis() {
            this.lenis = new Lenis({
                lerp: this.config.lerp,
                wheelMultiplier: this.config.wheelMultiplier,
                direction: this.config.orientation,
                smoothTouch: this.config.smoothTouch,
                gestureDirection: this.config.orientation,
            });

            window.sifencyLenis = this.lenis;
            
            document.documentElement.classList.add('lenis', 'lenis-smooth');

            this.integrateGSAP();
            this.handleAnchors();

            // Only start native RAF if GSAP is not handling it
            if (!(window.gsap && window.ScrollTrigger)) {
                this._rafStopped = false;
                this._boundRaf = this.raf.bind(this);
                requestAnimationFrame(this._boundRaf);
            }
        }

        raf(time) {
            if (this._rafStopped) return;
            this.lenis.raf(time);
            requestAnimationFrame(this._boundRaf);
        }

        handleAnchors() {
            document.querySelectorAll('a[href^="#"]').forEach(anchor => {
                anchor.addEventListener('click', (e) => {
                    const targetId = anchor.getAttribute('href');
                    if (targetId === '#' || !targetId) return;
                    
                    const targetElement = document.querySelector(targetId);
                    if (targetElement) {
                        e.preventDefault();
                        this.lenis.scrollTo(targetElement, {
                            offset: -50,
                            duration: 1.5
                        });
                    }
                });
            });
        }

        integrateGSAP() {
            if (window.gsap && window.ScrollTrigger) {
                this.lenis.on('scroll', ScrollTrigger.update);
                
                gsap.ticker.add((time) => {
                    this.lenis.raf(time * 1000);
                });
                
                gsap.ticker.lagSmoothing(0);
            }
        }
    }

    $(window).on('elementor/frontend/init', () => {
        new SifencyScrollManager();
    });

})(jQuery);