(function ($) {
    "use strict";
    (() => { var o = class { constructor(t) { this.data = t; this.style = "position: fixed; pointer-events: none; top: 0; left: 0;"; this.body = document.querySelector("body"); this.count = this.data.count || 1, this.targets = this.data.targets || !1, this.init(), this.move(), this.status() } init() { new Array(this.count).fill(0).forEach((e, s) => { let r = document.createElement("div"); this.create(r, s) }) } create(t, e) { t.setAttribute("data-cursor", `${e}`), t.setAttribute("style", this.style), this.body.append(t) } move() { let t = document.querySelectorAll("[data-cursor]"); document.addEventListener("mousemove", e => { let { clientX: s, clientY: r } = e; t.forEach(i => this.position(i, s, r)) }) } position(t, e, s) { t.style.transform = `translate3d(calc(${e}px - 50%), calc(${s}px - 50%), 0)` } status() { if (this.targets instanceof Array) for (let t of this.targets) { let e = document.querySelectorAll(t); for (let s of e) s.addEventListener("mouseover", this.hover.bind(this, t)), s.addEventListener("mouseleave", this.hover.bind(this, t)) } } hover(t) { let e = t.replace(/[.#!]/g, ""); this.body.classList.toggle(`cursor-hover--${e}`) } }; window.Cursor = o; })();

    const lerp = (a, b, n) => (1 - n) * a + n * b;

    const initSifencyCursor = ($scope) => {
        const settings = $scope.find('.sifency-modern-cursor-wrapper').data('settings');
        if (!settings) return;

        const cursorWrap = $scope.find('.sifency-modern-cursor')[0];
        if (!cursorWrap) return; // guard: widget markup not rendered yet

        const targetSelector = settings.target || 'body';
        const cursorSpeed = settings.speed || 0.15;

        if (settings.style === 'style-dual-anim') {
            cursorWrap.style.display = 'none';
            new window['Cursor']({ count: 2, targets: ['a', 'button', '.elementor-button'] });
            return;
        }

        const dot = cursorWrap.querySelector('.sifency-cursor-dot');
        const follower = cursorWrap.querySelector('.sifency-cursor-follower');
        let mouseX = 0, mouseY = 0, followerX = 0, followerY = 0;
        let isVisible = false;
        let cursorRafId = null; // store RAF ID for cancellation

        document.addEventListener('mousemove', (e) => {
            mouseX = e.clientX; mouseY = e.clientY;

            if (!isVisible) {
                followerX = mouseX; followerY = mouseY;
                isVisible = true;
            }

            if (dot) dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`;
        });

        const renderLoop = () => {
            followerX = lerp(followerX, mouseX, cursorSpeed);
            followerY = lerp(followerY, mouseY, cursorSpeed);
            if (follower) follower.style.transform = `translate3d(${followerX}px, ${followerY}px, 0)`;
            cursorRafId = requestAnimationFrame(renderLoop);
        };
        cursorRafId = requestAnimationFrame(renderLoop);

        // Stop the loop when the page is unloaded to prevent memory leaks
        window.addEventListener('beforeunload', () => {
            if (cursorRafId) cancelAnimationFrame(cursorRafId);
        }, { once: true });

        const interactives = document.querySelectorAll('a, button');
        interactives.forEach(el => {
            el.addEventListener('mouseenter', () => cursorWrap.classList.add('is-hovering'));
            el.addEventListener('mouseleave', () => cursorWrap.classList.remove('is-hovering'));
        });

        if (settings.click_effect === 'yes') {
            document.addEventListener('mousedown', () => cursorWrap.classList.add('is-clicking'));
            document.addEventListener('mouseup', () => cursorWrap.classList.remove('is-clicking'));
        }

        const hideDefault = settings.hide_default !== false;

        const targets = document.querySelectorAll(targetSelector);
        targets.forEach(target => {
            target.addEventListener('mousemove', () => {
                cursorWrap.style.opacity = '1';
                if (hideDefault) target.style.cursor = 'none';
            });
            target.addEventListener('mouseleave', () => {
                cursorWrap.style.opacity = '0';
                if (hideDefault) target.style.cursor = 'auto';
            });
        });
    };

    $(window).on('elementor/frontend/init', () => {
        elementorFrontend.hooks.addAction('frontend/element_ready/sifency-custom-cursor.default', initSifencyCursor);
    });
})(jQuery);