;(function($) {
    'use strict';
    var PriceMenuHandler = function($scope, $) {
        var $widgetWrapper = $scope.find('.unlockafe-price-menu-wrapper');
        if (!$widgetWrapper.length) return;
        
        // Check if we're in Elementor editor mode
        var isEditorMode = typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode;
        
        // --- 1. Scroll Animation ---
        var $animatedElements = $widgetWrapper.find('.animate-on-scroll');
        
        // In editor mode, show all elements immediately
        if (isEditorMode) {
            $animatedElements.addClass('is-visible');
            console.log('Price Menu: Made', $animatedElements.length, 'elements visible in editor mode');
        } else if (window.IntersectionObserver && $animatedElements.length > 0) {
            const observer = new IntersectionObserver((entries, obs) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        $(entry.target).addClass('is-visible');
                        obs.unobserve(entry.target);
                    }
                });
            }, { threshold: 0.1 });
            $animatedElements.each(function() { observer.observe(this); });
        }
        // --- 2. Carousel Initialization (Simplified) ---
        var $swiperContainer = $widgetWrapper.find('.swiper-container');
        if ($swiperContainer.length > 0) {
            var options = $swiperContainer.data('swiper-options');
            if (typeof options === 'undefined') return;

            var swiperOptions = {
                loop: options.loop || false,
                spaceBetween: options.space_between || 24,
                breakpoints: {
                    0: { slidesPerView: options.slides_mobile || 1 },
                    768: { slidesPerView: options.slides_tablet || 2 },
                    992: { slidesPerView: options.slides_desktop || 4 }
                }
            };

            if (options.navigation) {
                swiperOptions.navigation = {
                    nextEl: $scope.find('.swiper-button-next')[0],
                    prevEl: $scope.find('.swiper-button-prev')[0],
                };
            }

            if (options.pagination) {
                swiperOptions.pagination = {
                    el: $scope.find('.swiper-pagination')[0],
                    clickable: true,
                };
                
            }
            try {
                new Swiper($swiperContainer[0], swiperOptions);
            } catch (error) {
                console.log(error);
            }
        }
    };

    $(window).on('elementor/frontend/init', function() {
        // Try the newer Elementor hook format first
        if (elementorFrontend.hooks.addAction) {
            elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-price-menu.default', PriceMenuHandler);
        }
        // Fallback to the older hook format
        if (elementorFrontend.hooks.addAction) {
            elementorFrontend.hooks.addAction('frontend/element_init/unlockafe-price-menu.default', PriceMenuHandler);
        }
        // General hook for debugging - this will catch all widgets
        if (elementorFrontend.hooks.addAction) {
            elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope) {
                // Check if this is our price menu widget
                if ($scope.find('.unlockafe-price-menu-wrapper').length > 0) {
                    PriceMenuHandler($scope, $);
                }
            });
        }
        
        // Handle editor preview mode
        if (elementorFrontend.isEditMode) {
            // Initialize price menu in editor preview
            elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope) {
                if ($scope.find('.unlockafe-price-menu-wrapper').length) {
                    setTimeout(function() {
                        PriceMenuHandler($scope, $);
                    }, 100);
                }
            });
            
            // Also initialize on editor preview refresh
            $(window).on('elementor:preview:loaded', function() {
                setTimeout(function() {
                    $('.elementor-widget-unlockafe-price-menu').each(function() {
                        var $scope = $(this);
                        PriceMenuHandler($scope, $);
                    });
                }, 200);
            });
        }
    });
    
    // Initialize on document ready for non-Elementor pages and editor mode
    $(document).ready(function() {
        $('.elementor-widget-unlockafe-price-menu').each(function() {
            var $scope = $(this);
            PriceMenuHandler($scope, $);
        });
        
        // Also initialize in editor mode after a delay
        if (typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode) {
            setTimeout(function() {
                $('.elementor-widget-unlockafe-price-menu').each(function() {
                    var $scope = $(this);
                    var $widgetWrapper = $scope.find('.unlockafe-price-menu-wrapper');
                    if ($widgetWrapper.length) {
                        var $animatedElements = $widgetWrapper.find('.animate-on-scroll');
                        $animatedElements.addClass('is-visible');
                        console.log('Price Menu: Made', $animatedElements.length, 'elements visible on document ready');
                    }
                });
            }, 500);
        }
    });

})(jQuery);