document.addEventListener("DOMContentLoaded", function () {
    const menuToggle = document.querySelector(".menu-toggle");
    const overlay = document.querySelector(".menu-overlay");

    if (menuToggle) {
        menuToggle.addEventListener("click", () => {
            document.body.classList.toggle("menu-open");
        });
    }

    if (overlay) {
        overlay.addEventListener("click", () => {
            document.body.classList.remove("menu-open");
        });
    }

    document.querySelectorAll(".mobile-menu a").forEach(link => {
        link.addEventListener("click", () => {
            document.body.classList.remove("menu-open");
        });
    });

    const gallery = document.querySelector(".gallery-slider");

    if (gallery) {
        new Splide(gallery, {
            type: "loop",
            perPage: 3,
            perMove: 1,
            gap: "14px",
            autoplay: true,
            interval: 3000,
            pauseOnHover: true,
            pauseOnFocus: false,
            arrows: true,
            pagination: false,
            speed: 800,

            breakpoints: {
                1024: {
                    perPage: 2,
                },
                767: {
                    perPage: 1,
                }
            }
        }).mount();
    }

    const items = document.querySelectorAll(".video-item");
    if (items) {
        const player = document.getElementById("video-player");

        let currentThumb = items[0].dataset.thumb;
        let currentIframe = items[0].dataset.iframe;

        function showPoster() {
            player.innerHTML = `
            <div class="video-poster">
                <img src="${currentThumb}" alt="">
                <button class="play-btn" type="button">
                    <img src="./assets/images/youtube-play-btn.svg" alt="">
                </button>
            </div>
        `;

            player.querySelector(".play-btn").addEventListener("click", playVideo);
        }

        function playVideo() {

            player.innerHTML = currentIframe;

            const iframe = player.querySelector("iframe");

            if (iframe) {
                const url = new URL(iframe.src);

                url.searchParams.set("autoplay", "1");
                url.searchParams.set("rel", "0");

                iframe.src = url.toString();
            }
        }

        // Initial Poster
        showPoster();

        // Left List Click
        items.forEach(item => {

            item.addEventListener("click", () => {

                items.forEach(i => i.classList.remove("active"));
                item.classList.add("active");

                currentThumb = item.dataset.thumb;
                currentIframe = item.dataset.iframe;

                // Sirf thumbnail update hogi
                showPoster();

            });

        });
    }


    const testimonialSlider = document.querySelector(".testimonial-slider");

    if (testimonialSlider) {
        new Splide(testimonialSlider, {
            type: "loop",
            perPage: 4,
            perMove: 1,
            gap: "20px",
            autoplay: true,
            interval: 3000,
            pauseOnHover: true,
            pauseOnFocus: false,
            arrows: true,
            pagination: false,
            speed: 800,

            breakpoints: {
                1200: {
                    perPage: 3,
                },
                1024: {
                    perPage: 2,
                },
                767: {
                    perPage: 1,
                }
            }
        }).mount();
    }

});