var SifencyFunFact = function($scope, $) {
    var counterElements = $scope.find('.sifency-counter');
    
    if ( ! counterElements.length ) {
        return;
    }

    var observer = new IntersectionObserver(function(entries, observer) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                var $counter = $(entry.target);
                
                var settings = {
                    toValue: parseFloat($counter.data('to-value')),
                    duration: parseInt($counter.data('duration'), 10) || 2000
                };

                $({ count: 0 }).animate({
                    count: settings.toValue
                }, {
                    duration: settings.duration,
                    easing: 'swing',
                    step: function() {
                        $counter.text(Math.floor(this.count).toLocaleString());
                    },
                    complete: function() {
                        $counter.text(this.count.toLocaleString());
                    }
                });
                
                observer.unobserve(entry.target);
            }
        });
    }, { 
        threshold: 0.8 
    });

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

jQuery(window).on('elementor/frontend/init', function () {
    elementorFrontend.hooks.addAction(
        'frontend/element_ready/sifency-addons-funfact.default', 
        SifencyFunFact
    );
});