(function ($) {
    'use strict';

    var Sifency_Marquee = function ($scope, $) {

        var $slider = $scope.find('.marquee');
        if (!$slider.length) return;

        var $content = $slider.find('.marquee-content');
        if ($content.children().length < 1) return;

        // Settings
        var direction = $slider.data('direction') || 'left';
        var speed = parseFloat($slider.data('speed')) || 20;
        var loopCount = $slider.data('loop-count') || "infinite";

        // Clone content for smooth looping
        var $clone = $content.clone().addClass('marquee-clone');
        $slider.append($clone);

        // Orientation
        var horizontal = (direction === 'left' || direction === 'right');

        function updateDimensions() {
            var size = 0;

            // Calculate full width/height of original content
            $content.children().each(function () {
                size += horizontal ? $(this).outerWidth(true) : $(this).outerHeight(true);
            });

            if (horizontal) {
                $content.css('width', size);
                $clone.css('width', size);
                $slider.css('width', size * 2);
            } else {
                $content.css('height', size);
                $clone.css('height', size);
                $slider.css('height', size * 2);
            }

            applyAnimation(size);
        }

        function applyAnimation(size) {
            var distance = size; 
            var duration = speed;

            // Base animation config
            var keyframes = {};

            if (direction === 'left') {
                keyframes = {
                    transformStart: 'translateX(0)',
                    transformEnd: 'translateX(-' + distance + 'px)'
                };
            }
            if (direction === 'right') {
                keyframes = {
                    transformStart: 'translateX(-' + distance + 'px)',
                    transformEnd: 'translateX(0)'
                };
            }
            if (direction === 'up') {
                keyframes = {
                    transformStart: 'translateY(0)',
                    transformEnd: 'translateY(-' + distance + 'px)'
                };
            }
            if (direction === 'down') {
                keyframes = {
                    transformStart: 'translateY(-' + distance + 'px)',
                    transformEnd: 'translateY(0)'
                };
            }

            // Inject keyframe dynamically (avoids CSS conflicts)
            var styleId = "sifency-marquee-style";
            $("#" + styleId).remove();

            $('<style id="' + styleId + '">')
                .html(`
                @keyframes sifency_marquee_anim {
                    0% { transform: ${keyframes.transformStart}; }
                    100% { transform: ${keyframes.transformEnd}; }
                }`)
                .appendTo("head");

            // Apply animation
            $slider.css({
                'overflow': 'hidden',
                'position': 'relative',
                'display': 'flex',
                'flex-direction': horizontal ? 'row' : 'column',
                'animation': `sifency_marquee_anim ${duration}s linear ${loopCount}`
            });
        }

        // Initial run
        updateDimensions();

        // Recalculate on resize to avoid breaking layout
        var resizeTimer;
        $(window).on('resize', function () {
            clearTimeout(resizeTimer);
            resizeTimer = setTimeout(updateDimensions, 300);
        });

    };

    $(window).on('elementor/frontend/init', function () {
        elementorFrontend.hooks.addAction(
            'frontend/element_ready/sifency-widget-text-slider.default',
            Sifency_Marquee
        );
    });

})(jQuery);
