!function (t) {
    "use strict";
    let n = {
        animationsInitialized: !1,
        init() {
            this.initAnimations();
            this.initFiltering();
            this.bindElementorHooks()
        },
        initFiltering() {
            t(document).on('click', '.sifency-addons-filter-item', function () {
                const $btn = t(this);
                const filterValue = $btn.data('filter');
                const $container = $btn.closest('.sifency-portfolio-wrapper').find('.sifency-filter-container');

                // Active class toggle
                $btn.addClass('active').siblings().removeClass('active');

                if (!$container.length) return;

                const $items = $container.children();

                if (filterValue === '*') {
                    $items.fadeIn(400);
                } else {
                    $items.hide();
                    $container.find(filterValue).fadeIn(400);
                }
            });
        },
        bindElementorHooks() {
            const register = () => {
                elementorFrontend.hooks.addAction("frontend/element_ready/global", $scope => {
                    this.initGlobalFloatingCount($scope);
                });
                elementorFrontend.hooks.addAction("frontend/element_ready/sifency-product-short-description.default", t => {
                    this.handleContentWrapperAnimations(t)
                });
            };

            if (window.elementorFrontend && typeof window.elementorFrontend.hooks !== 'undefined') {
                register();
            } else {
                t(window).on("elementor/frontend/init", register);
            }
        },
        initGlobalFloatingCount($scope) {
            // Only initialize on the actual widget scope, not columns or sections
            if (!$scope.hasClass('elementor-widget')) return;

            if ($scope.data('sifency-floating-count-initialized') === 'yes') return;

            const floatingCount = $scope.find('.sifency-floating-count-num');
            if (!floatingCount.length) return;

            $scope.data('sifency-floating-count-initialized', 'yes');

            const swiperElement = $scope.find('.swiper-container, .swiper')[0];
            const isCarousel = !!swiperElement;

            let itemSelector = '';
            if (isCarousel) {
                itemSelector = '.swiper-slide';
            } else {
                itemSelector = '.sifency-service-item, .sifency-addons-reveal-card, .sifency-blog-item, .sifency-portfolio-item, .sifency-team-item, .sifency-testimonial-item';
            }

            const items = $scope.find(itemSelector)
                .not('.swiper-slide-duplicate, .swiper-slide-duplicate *');

            if (!items.length) return;

            const widgetId = $scope.attr('data-id') || Math.random().toString(36).substr(2, 9);
            const scrollNs = 'scroll.sifencyFloatingCount_' + widgetId;
            const resizeNs = 'resize.sifencyFloatingCount_' + widgetId;

            t(window).off(scrollNs + ' ' + resizeNs);

            const bindScrollSpy = () => {
                const updateFloatingCount = () => {
                    let activeIndex = 0;
                    const viewportCenter = window.innerHeight / 2;

                    // 1. If scrolled to the absolute bottom of the page
                    if (window.innerHeight + window.pageYOffset >= document.documentElement.scrollHeight - 50) {
                        activeIndex = items.length - 1;
                    }
                    // 2. Otherwise use the unified Trigger-Line Scroll-Spy
                    else {
                        items.each(function (index) {
                            const rect = this.getBoundingClientRect();
                            const $item = t(this);

                            const isSticky = $item.css('position') === 'sticky';
                            let triggerLine = viewportCenter;

                            if (isSticky) {
                                const stickyTop = parseInt($item.css('top'), 10) || 80;
                                triggerLine = stickyTop + 30; // 30px tolerance for active stack trigger
                            }

                            if (rect.top <= triggerLine) {
                                activeIndex = index;
                            }
                        });
                    }

                    const prefix = floatingCount.attr('data-prefix') || '';
                    const formattedNum = prefix + (activeIndex + 1);
                    if (floatingCount.text() !== formattedNum) {
                        floatingCount.text(formattedNum);
                    }
                };

                updateFloatingCount();
                t(window).on(scrollNs + ' ' + resizeNs, updateFloatingCount);

                // Periodic fallback check for Lenis / hijacked scrollbars
                const intervalId = setInterval(updateFloatingCount, 250);

                $scope[0].addEventListener('sifency:destroy', () => {
                    clearInterval(intervalId);
                    t(window).off(scrollNs + ' ' + resizeNs);
                });
            };

            if (isCarousel) {
                let attempts = 0;
                const initSwiperFloatingCount = () => {
                    const swiperInstance = swiperElement.__swiperInstance || swiperElement.swiper;
                    if (swiperInstance) {
                        const updateCarouselCount = () => {
                            let activeIndex = swiperInstance.realIndex;
                            if (activeIndex === undefined) {
                                const totalSlides = swiperInstance.slides.not('.swiper-slide-duplicate').length;
                                activeIndex = swiperInstance.activeIndex;
                                if (swiperInstance.params.loop && totalSlides > 0) {
                                    activeIndex = swiperInstance.activeIndex - swiperInstance.loopedSlides;
                                    if (activeIndex < 0) {
                                        activeIndex = totalSlides + activeIndex;
                                    } else if (activeIndex >= totalSlides) {
                                        activeIndex = activeIndex - totalSlides;
                                    }
                                }
                            }
                            const prefix = floatingCount.attr('data-prefix') || '';
                            const formattedNum = prefix + ((activeIndex || 0) + 1);
                            floatingCount.text(formattedNum);
                        };
                        swiperInstance.on('slideChange', updateCarouselCount);
                        swiperInstance.on('transitionEnd', updateCarouselCount);
                        updateCarouselCount();
                    } else {
                        attempts++;
                        if (attempts < 30) { // try for 1.5 seconds
                            setTimeout(initSwiperFloatingCount, 50);
                        } else {
                            bindScrollSpy();
                        }
                    }
                };
                initSwiperFloatingCount();
            } else {
                bindScrollSpy();
            }
        },
        handleContentWrapperAnimations(t) {
            let n = t[0].querySelector(".sifency-product-short-description[data-aos]");
            if (!n) return;

            let isEditMode = elementorFrontend.isEditMode();

            let e = Array.from(n.children),
                i = parseInt(n.dataset.aosDelay, 10) || 0;

            if (0 === e.length && "" !== n.innerHTML.trim()) {
                let a = n.innerHTML;
                n.innerHTML = "";
                let o = document.createElement("div");
                o.className = "sifency-inner-content";
                o.innerHTML = a;
                n.appendChild(o);
                e = [o]
            }

            if (0 !== e.length) {
                e.forEach((t, e) => {
                    if (!isEditMode) {
                        for (let a in n.dataset) {
                            if (a.startsWith("aos")) {
                                t.dataset[a] = n.dataset[a];
                            }
                        }
                        t.dataset.aosDelay = i + 100 * e;
                    } else {
                        t.style.opacity = "1";
                    }
                });

                if (!isEditMode) {
                    for (let s in n.dataset) s.startsWith("aos") && delete n.dataset[s];
                    "undefined" != typeof AOS && AOS.refresh();
                }
            }
        },
        initAnimations() {
            if (!this.animationsInitialized) {
                if ("undefined" != typeof AOS && "function" == typeof AOS.init) {
                    AOS.init({
                        disable: elementorFrontend.isEditMode()
                    });
                }
                "undefined" != typeof WOW && "function" == typeof WOW && new WOW().init();
                this.animationsInitialized = !0;
            }
        }
    };
    if (window.elementorFrontend && typeof window.elementorFrontend.hooks !== 'undefined') {
        n.init();
    } else {
        t(window).on("elementor/frontend/init", () => {
            n.init();
        });
    }
}(jQuery);