;(function ($) {
    "use strict";

    var UATimeline = function ($scope, $) {
        // Find all timeline items (vertical, horizontal, and simple vertical)
        const items = $scope.find('.v-timeline-item, .h-timeline-event');

        if (!items.length) {
            return;
        }

        // Check if we're in Elementor editor mode
        const isEditorMode = typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode;

        const isInViewport = el => {
            // In editor mode, always show items
            if (isEditorMode) {
                return true;
            }
            const rect = el.getBoundingClientRect();
            // A bit more lenient to trigger animation earlier
            return (
                rect.top <= (window.innerHeight || document.documentElement.clientHeight) - 50 && // Trigger 50px before it's fully visible
                rect.bottom >= 50 // Un-trigger 50px after it's out of view
            );
        };

        const run = () => {
            items.each(function () {
                if (isInViewport(this)) {
                    $(this).addClass('is-visible');
                }
            });
        }

        // In editor mode, show all items immediately
        if (isEditorMode) {
            items.addClass('is-visible');
            console.log('Event Timeline: Made', items.length, 'items visible in editor mode');
        } else {
            // Run on load, resize, and scroll
            $(window).on('load resize scroll', run);
            
            // Initial run in case some items are already in view
            run();
        }
    };

    $(window).on('elementor/frontend/init', function () {
        elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-time-line.default', UATimeline);
        
        // Handle editor preview mode
        if (elementorFrontend.isEditMode) {
            // Initialize timeline in editor preview
            elementorFrontend.hooks.addAction('frontend/element_ready/global', function ($scope) {
                if ($scope.find('.v-timeline-item, .h-timeline-event').length) {
                    setTimeout(function() {
                        UATimeline($scope, $);
                    }, 100);
                }
            });
            
            // Also initialize on editor preview refresh
            $(window).on('elementor:preview:loaded', function() {
                setTimeout(function() {
                    $('.elementor-widget-unlockafe-time-line').each(function() {
                        var $scope = $(this);
                        UATimeline($scope, $);
                    });
                }, 200);
            });
            
            // Multiple initialization attempts for editor mode
            var initAttempts = 0;
            var maxAttempts = 10;
            var tryInit = function() {
                initAttempts++;
                $('.elementor-widget-unlockafe-time-line').each(function() {
                    var $scope = $(this);
                    var $items = $scope.find('.v-timeline-item, .h-timeline-event');
                    if ($items.length > 0) {
                        $items.addClass('is-visible');
                    }
                });
                if (initAttempts < maxAttempts) {
                    setTimeout(tryInit, 200);
                }
            };
            setTimeout(tryInit, 300);
            setTimeout(tryInit, 600);
            setTimeout(tryInit, 1000);
        }
    });
    
    // Initialize on document ready for non-Elementor pages and editor mode
    $(document).ready(function() {
        $('.elementor-widget-unlockafe-time-line').each(function() {
            var $scope = $(this);
            UATimeline($scope, $);
        });
        
        // Also initialize in editor mode after a delay
        if (typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode) {
            setTimeout(function() {
                $('.elementor-widget-unlockafe-time-line').each(function() {
                    var $scope = $(this);
                    var $items = $scope.find('.v-timeline-item, .h-timeline-event');
                    if ($items.length > 0) {
                        $items.addClass('is-visible');
                        console.log('Event Timeline: Made', $items.length, 'items visible on document ready');
                    }
                });
            }, 500);
        }
    });
})(jQuery);