(function($) {
    const initTestimonialsCarousel = function($scope) {
        const carousel = $scope.find('.okthemes-testimonials-carousel')[0];
        if (!carousel) return;

        const settings = {
            navigation:            carousel.dataset.navigation || 'both',
            autoplay:              carousel.dataset.autoplay === 'yes',
            autoplaySpeed:         parseInt(carousel.dataset.autoplaySpeed) || 5000,
            loop:                  carousel.dataset.loop === 'yes',
            transitionSpeed:       parseInt(carousel.dataset.transitionSpeed) || 500,
            pauseOnHover:          carousel.dataset.pauseOnHover === 'true',
            slidesPerView:         parseInt(carousel.dataset.slidesPerView) || 1,
            slidesPerViewTablet:   carousel.dataset.slidesPerViewTablet || '',
            slidesPerViewMobile:   carousel.dataset.slidesPerViewMobile || '',
            spaceBetween:          parseInt(carousel.dataset.spaceBetween) || 30,
        };

        const totalSlides    = carousel.querySelectorAll('.swiper-slide').length;
        const canLoop        = settings.loop && totalSlides >= settings.slidesPerView * 2;

        const breakpoints = {};
        if (settings.slidesPerViewMobile) {
            breakpoints[320] = { slidesPerView: parseInt(settings.slidesPerViewMobile) };
        }
        if (settings.slidesPerViewTablet) {
            breakpoints[768] = { slidesPerView: parseInt(settings.slidesPerViewTablet) };
        }

        const swiperOptions = {
            slidesPerView: settings.slidesPerView,
            spaceBetween:  settings.spaceBetween,
            loop:          canLoop,
            speed:         settings.transitionSpeed,
            autoplay:      settings.autoplay ? { delay: settings.autoplaySpeed, disableOnInteraction: settings.pauseOnHover } : false,
            breakpoints:   Object.keys(breakpoints).length ? breakpoints : undefined,
        };

        if (settings.navigation === 'arrows' || settings.navigation === 'both') {
            swiperOptions.navigation = {
                nextEl: carousel.querySelector('.swiper-button-next'),
                prevEl: carousel.querySelector('.swiper-button-prev'),
            };
        }

        if (settings.navigation === 'dots' || settings.navigation === 'both') {
            swiperOptions.pagination = {
                el: carousel.querySelector('.swiper-pagination'),
                clickable: true,
            };
        }

        const swiper = new Swiper(carousel, swiperOptions);

        if (settings.autoplay && settings.pauseOnHover) {
            carousel.addEventListener('mouseenter', function() { swiper.autoplay.stop(); });
            carousel.addEventListener('mouseleave', function() { swiper.autoplay.start(); });
        }
    };

    $(window).on('elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction('frontend/element_ready/okthemes-testimonials-carousel.default', initTestimonialsCarousel);
    });
})(jQuery);
