;(function($) {
    'use strict';
    
    // Function to initialize Customer Review widget
    function initCustomerReview($scope) {
        // Wait for DOM to be ready
        setTimeout(function() {
            // Animation for layout 1, 2, 3
            const animatedElements = $scope.find('#layout1 .individual-review-card, #layout2 .review-card, #layout3 .review-card-container');
            if (animatedElements.length) {
                const observer = new IntersectionObserver((entries) => {
                    entries.forEach((entry, index) => {
                        if (entry.isIntersecting) {
                            setTimeout(() => {
                                entry.target.classList.add('is-visible');
                            }, index * 100);
                            observer.unobserve(entry.target);
                        }
                    });
                }, { threshold: 0.1 });

                animatedElements.each(function() {
                    observer.observe(this);
                });
            }

            // Tilt effect for layout 3
            const tiltCards = $scope.find('#layout3 .review-card');
            if (tiltCards.length) {
                tiltCards.each(function() {
                    const card = $(this);
                    card.off('mousemove mouseleave'); // Remove existing handlers
                    card.on('mousemove', function(e) {
                        const rect = this.getBoundingClientRect();
                        const x = e.clientX - rect.left;
                        const y = e.clientY - rect.top;
                        const centerX = rect.width / 2;
                        const centerY = rect.height / 2;
                        const rotateX = (y - centerY) / 15;
                        const rotateY = (centerX - x) / 15;
                        this.style.transform = `rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
                    });
                    card.on('mouseleave', function() {
                        this.style.transform = 'rotateX(0) rotateY(0)';
                        if (card.hasClass('highlight-card')) {
                            this.style.transform = 'scale(1.05) rotateX(0) rotateY(0)';
                        }
                    });
                });
            }

            // Swiper initialization for Layout 4
            const $swiperContainer4 = $scope.find('#layout4 .swiper-container');
            if ($swiperContainer4.length > 0) {
                // Check if Swiper is already initialized
                if ($swiperContainer4[0].swiper) {
                    $swiperContainer4[0].swiper.destroy(true, true);
                }

                const userOptions = $swiperContainer4.data('swiper-options') || {};
                const $pagination4 = $scope.find('#layout4 .swiper-pagination');
                
                const swiperOptions4 = $.extend({
                    grabCursor: true,
                    speed: 600,
                    observer: true,
                    observeParents: true,
                    breakpoints: {
                        320: { slidesPerView: 1, spaceBetween: 20 },
                        768: { slidesPerView: 2, spaceBetween: 30 },
                        1024: { slidesPerView: 3, spaceBetween: 40 }
                    }
                }, userOptions);

                // Add pagination if it exists
                if ($pagination4.length > 0) {
                    swiperOptions4.pagination = {
                        el: $pagination4[0],
                        clickable: true,
                        type: 'bullets',
                    };
                }

                if (swiperOptions4.autoplay && swiperOptions4.autoplay.delay) {
                    swiperOptions4.autoplay.disableOnInteraction = false;
                }

                try {
                    const swiper4 = new Swiper($swiperContainer4[0], swiperOptions4);
                    swiper4.update();
                    swiper4.updateSlides();
                    swiper4.updateSlidesClasses();
                } catch (error) {
                    console.error('Swiper Layout 4 initialization error:', error);
                }
            }

            // Swiper initialization for Layout 5
            const $swiperContainer5 = $scope.find('#layout5 .swiper-container');
            if ($swiperContainer5.length > 0) {
                // Check if Swiper is already initialized
                if ($swiperContainer5[0].swiper) {
                    $swiperContainer5[0].swiper.destroy(true, true);
                }

                const userOptions5 = $swiperContainer5.data('swiper-options') || {};
                const $pagination5 = $scope.find('#layout5 .swiper-pagination');
                
                const swiperOptions5 = $.extend({
                    effect: 'coverflow',
                    grabCursor: true,
                    centeredSlides: true,
                    slidesPerView: 'auto',
                    speed: 600,
                    observer: true,
                    observeParents: true,
                    coverflowEffect: {
                        rotate: 0,
                        stretch: 0,
                        depth: 200,
                        modifier: 1,
                        slideShadows: false,
                    },
                }, userOptions5);

                // Add pagination if it exists
                if ($pagination5.length > 0) {
                    swiperOptions5.pagination = {
                        el: $pagination5[0],
                        clickable: true,
                        type: 'bullets',
                    };
                }
            
                if (swiperOptions5.autoplay && swiperOptions5.autoplay.delay) {
                    swiperOptions5.autoplay.disableOnInteraction = false;
                }

                try {
                    const swiper5 = new Swiper($swiperContainer5[0], swiperOptions5);
                    swiper5.update();
                    swiper5.updateSlides();
                    swiper5.updateSlidesClasses();
                } catch (error) {
                    console.error('Swiper Layout 5 initialization error:', error);
                }
            }
        }, 100);
    }

    // Initialize on Elementor frontend
    $(window).on('elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-customer-review.default', function($scope) {
            initCustomerReview($scope);
        });
    });

    // Also initialize on document ready (for non-Elementor pages or after page load)
    $(document).ready(function() {
        // Wait for Swiper to be available
        function checkSwiperAndInit() {
            if (typeof Swiper !== 'undefined') {
                $('.elementor-widget-unlockafe-customer-review').each(function() {
                    var $scope = $(this);
                    initCustomerReview($scope);
                });
            } else {
                // Retry after a short delay if Swiper is not yet loaded
                setTimeout(checkSwiperAndInit, 100);
            }
        }
        checkSwiperAndInit();
    });
})(jQuery);