document.addEventListener("DOMContentLoaded", () => {

    // 1. LENIS - AKICI SCROLL MOTORU AKTİF
    const lenis = new Lenis({
        duration: 1.1,
        easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
        smoothWheel: true
    });

    function raf(time) {
        lenis.raf(time);
        requestAnimationFrame(raf);
    }
    requestAnimationFrame(raf);

    // 2. PRELOADER SAYAÇ VE SAYFAYI GÖSTERME MOTORU
    let count = 0;
    const preloaderText = document.querySelector(".preloader-text");
    const preloaderOverlay = document.getElementById("custom-preloader");

    const counterInterval = setInterval(() => {
        count += Math.floor(Math.random() * 12) + 2;
        
        if (count >= 100) {
            count = 100;
            clearInterval(counterInterval);
            
            // Preloader'ı Kapat ve Sayfayı Görünür Yap
            if (preloaderOverlay) {
                preloaderOverlay.style.opacity = "0";
                preloaderOverlay.style.visibility = "hidden";
                setTimeout(() => {
                    runHeroAnimations();
                    initScrollAnimations();
                }, 200);
            }
        }
        if (preloaderText) {
            preloaderText.innerText = count + "%";
        }
    }, 50);

    // 3. HERO GİRİŞ ANİMASYONLARI (GSAP)
    function runHeroAnimations() {
        gsap.from(".animate-hero", {
            y: 50,
            opacity: 0,
            duration: 1,
            stagger: 0.2,
            ease: "power3.out"
        });
    }

    // 4. SCROLL ANİMASYONLARI
    function initScrollAnimations() {
        gsap.registerPlugin(ScrollTrigger);

        gsap.utils.toArray(".scroll-animate").forEach((section) => {
            gsap.fromTo(section, 
                { opacity: 0, y: 40 },
                {
                    opacity: 1,
                    y: 0,
                    duration: 1,
                    ease: "power3.out",
                    scrollTrigger: {
                        trigger: section,
                        start: "top 85%",
                        toggleActions: "play none none none"
                    }
                }
            );
        });
    }

    // 5. SAYFALAR ARASI GEÇİŞ (BARBA.JS)
    barba.init({
        sync: true,
        transitions: [{
            async leave(data) {
                const done = this.async();
                gsap.to(".transition-wipe", {
                    duration: 0.5,
                    scaleY: 1,
                    transformOrigin: "bottom",
                    ease: "power2.inOut"
                });
                setTimeout(done, 500);
            },
            async enter(data) {
                gsap.to(".transition-wipe", {
                    duration: 0.5,
                    scaleY: 0,
                    transformOrigin: "top",
                    ease: "power2.inOut"
                });
                runHeroAnimations();
                initScrollAnimations();
                ScrollTrigger.refresh();
            }
        }]
    });
});