(function($){

    var SifencyVerticalProgressHandler = function($scope, $) {
        
        var $wrapper = $scope.find('.sifency-vprog-wrapper');
        if (!$wrapper.length) return;

        var $bars = $wrapper.find('.sifency-vprog-fill');

        var animate = function() {
            $bars.each(function() {
                var $bar = $(this);
                var h = $bar.data('height');

                // Force repaint so transition works
                $bar[0].offsetHeight;

                // Apply correct height
                $bar.css('height', h + '%');

                $bar.closest('.sifency-vprog-item').addClass('is-animated');
            });
        };

        // Replace deprecated waypoint with IntersectionObserver
        var observer = new IntersectionObserver(function(entries){
            entries.forEach(function(entry){
                if (entry.isIntersecting) {
                    animate();
                    observer.disconnect();
                }
            });
        }, { threshold: 0.2 });

        observer.observe($wrapper[0]);
    };

    $(window).on('elementor/frontend/init', function(){
        elementorFrontend.hooks.addAction(
            'frontend/element_ready/sifency_vertical_progress.default',
            SifencyVerticalProgressHandler
        );
    });

})(jQuery);
