window.L2001 = function () {
    if (window["runParalax"] !== undefined) {
        window["runParalax"]();
    }
    const swiper2 = new Swiper('.ourBlogSwiper1', {
        slidesPerView: 1,
        spaceBetween: 0,
        autoplay: {
            delay: 2000,      // 5s
            disableOnInteraction: true,
        },
        loop: true,  // bật loop để chạy liên tục
        speed: 1000,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        loopedSlides: 5,  // số slide duplicate
        breakpoints: {
            1400: { slidesPerView: 4 },
            992: { slidesPerView: 3 },  // màn hình ≥1200px
            576: { slidesPerView: 2 },  // ≥768px=
            0: { slidesPerView: 1 }   // <576px
        },
    });


    const parallaxEls = document.querySelectorAll('.con-perf-section .parallax');
    let ticking = false;
    parallaxEls.forEach(el => {
        const parent = el.closest(".con-parallax");
        if (parent) {
            parent.style.height = parent.querySelector("[data-content]").offsetHeight + "px";
        }
    });
    const countNum = document.querySelectorAll('[CountNumber]');
    let tickingC = false;
    window.app.on("App_Scrolling", (t) => {
        if (!ticking) {
            window.requestAnimationFrame(() => {
                updateParallax(t, parallaxEls);
                ticking = false;
            });
            ticking = true;
        }
        if (!tickingC) {
            window.requestAnimationFrame(() => {
                countNum.forEach((el) => {
                    animateCount(el);
                })
                tickingC = false;
            });
            tickingC = true;
        }
    });

}
function animateCount(el) {
    const rect = el.getBoundingClientRect();
    const inView = rect.bottom > 0 && rect.top < window.innerHeight;
    if (el.hasAttribute("render")) {
        if (!inView) {
            el.removeAttribute("render");
        }
        return;
    }
    if (inView) {
        el.setAttribute("render", "");
        const min = parseInt(el.getAttribute("num-min") ?? 0);
        const max = parseInt(el.getAttribute("num-max") ?? 1);
        const prefix = el.getAttribute("prefix") ?? "";
        const duration = parseInt(el.getAttribute("duration") ?? 1000); // ms
        const interval = 10; // ms mỗi tick

        const steps = duration / interval;
        const stepValue = Math.ceil((max - min) / steps);

        let current = min;
        let tickCount = 0;

        function tick() {
            current = Math.min(min + stepValue * tickCount, max);
            el.textContent = prefix + current.toLocaleString("en-US");
            tickCount++;

            if (tickCount <= steps) {
                window.requestTimeout(tick, interval);
            }
        }

        tick(); // khởi chạy
    }
}

function updateParallax(t, parallaxEls) {
    parallaxEls.forEach(el => {
        const section = el.closest('.con-parallax');
        if (!section) return;

        const rect = section.getBoundingClientRect();
        const sectionHeight = section.offsetHeight;

        // Only process if the section is in the viewport or partially in it
        const inView = rect.bottom > 0 && rect.top < window.innerHeight;
        if (!inView) return;

        // Set height parent only once
        const content = section.querySelector('[data-content]');
        if (content && !section.dataset.heightSet) {
            section.style.height = content.offsetHeight + 'px';
            section.dataset.heightSet = 'true'; // Mark that the height is set
            section.dataset.initialTop = rect.top;
        }

        const img = el.querySelector('.bg-img');
        if (!img) return;

        const imgHeight = img.offsetHeight;
        const depth = parseFloat(el.dataset.depth ?? 1); // 1 = moves fully, <1 = moves less
        const initialTop = parseFloat(section.dataset.initialTop);

        // Calculate how far the section has moved from its initial position
        // Calculate progress for how much of the section is visible in the viewport
        let a = 0
        if (window.innerHeight - initialTop > 100) {
            a = window.innerHeight - initialTop - rect.top - rect.bottom;
        } else {
            a = window.innerHeight - rect.top
        }
        const progress = Math.min(1, Math.max(0, a / (window.innerHeight + sectionHeight)));
        const scrollable = imgHeight - sectionHeight;

        let offsetY = -progress * scrollable * depth;
        img.style.transform = `translate(-50%, ${offsetY}px)`;
    });
}
window.AScript.set("2001", true);