
function isInViewport(element) {
    const rect = element.getBoundingClientRect();
    return (
        rect.top < window.innerHeight &&
        rect.bottom > 0 &&
        rect.left < window.innerWidth &&
        rect.right > 0
    );
}
function updateActiveSlideShow(swiperInstance) {
    swiperInstance.slides.forEach(slide => slide.classList.remove('show'));

    const activeSlide = swiperInstance.slides[swiperInstance.activeIndex];
    if (isInViewport(activeSlide)) {
        activeSlide.classList.add('show');
    }
}

window.L1000 = function () {
    const swiper = new Swiper('.mainSlider', {
        loop: true,
        on: {
            slideChangeTransitionEnd: function () {
                updateActiveSlideShow(this);
            }
        }
    });
    document.addEventListener('visibilitychange', function () {
        if (document.visibilityState === 'hidden') {
            // Remove class show khỏi tất cả các slide
            swiper.slides.forEach(slide => slide.classList.remove('show'));
        }

        if (document.visibilityState === 'visible') {
            // Add class show lại cho slide đang active
            const activeSlide = swiper.slides[swiper.activeIndex];
            if (activeSlide) {
                // Delay một chút để CSS animation có thời gian reset
                window.requestTimeout(() => {
                    activeSlide.classList.add('show');
                }, 50); // delay nhẹ để đảm bảo browser detect lại class
            }
        }
    });
}
window.AScript.set("1000", true);