(function ($) {
    "use strict";

    var SifencyProgressBar = function ($scope, $) {
        var $progressBar = $scope.find('.sifency-skill-bar-fill');

        if (!$progressBar.length) {
            return;
        }

        var observer = new IntersectionObserver(function (entries) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    var $bar = $(entry.target);
                    var percentage = $bar.data('percentage');
                    $bar.css('width', percentage + '%');
                    observer.unobserve(entry.target);
                }
            });
        }, {
            threshold: 0.5
        });

        $progressBar.each(function () {
            observer.observe(this);
        });
    };

    $(window).on('elementor/frontend/init', function () {
        elementorFrontend.hooks.addAction('frontend/element_ready/sifency-team-skills.default', SifencyProgressBar);
    });

    var SifencyFrontend = {
        eventsBound: false,
        init: function () {
            this.bindWidgetHandlers();
        },

        bindWidgetHandlers: function () {
            elementorFrontend.hooks.addAction('frontend/element_ready/progressbar.default', this.widgetProgressBarHandler);
        },
        widgetProgressBarHandler: function ($scope) {
            var progressBar = $scope.find('.sifency-progress-bar__fill');
            if (!progressBar.length) return;

            var observer = new IntersectionObserver(function (entries) {
                entries.forEach(function (entry) {
                    if (entry.isIntersecting) {
                        var $bar = $(entry.target);
                        $bar.css('width', $bar.data('percent') + '%');
                        observer.unobserve(entry.target);
                    }
                });
            }, { threshold: 0.8 });

            progressBar.each(function () {
                observer.observe(this);
            });
        }
    };
    $(window).on('elementor/frontend/init', SifencyFrontend.init.bind(SifencyFrontend));


})(jQuery);