
(function () {
    'use strict';

    var reduced = false;
    try {
        reduced = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    } catch (e) {}

    var instances = [];
    var rafId = null;

    
    
    function getDeviceSens(section) {
        var w = window.innerWidth;
        if (w <= 767) {
            var m = section.getAttribute('data-bg-parallax-sens-mobile');
            if (m !== null && m !== '') return parseInt(m, 10) || 0;
        }
        if (w <= 1024) {
            var t = section.getAttribute('data-bg-parallax-sens-tablet');
            if (t !== null && t !== '') return parseInt(t, 10) || 0;
        }
        return parseInt(section.getAttribute('data-bg-parallax-sens'), 10) || 0;
    }

    function initSection(section) {
        if (section._bgParallaxInit) return;
        if (reduced) return;

        if (section.hasAttribute('data-depth-image') && section.hasAttribute('data-depth-map')) return;
        var sens = getDeviceSens(section);
        if (sens <= 0) return;
        var layer = section.querySelector(':scope > .impulse-section-bg-image-parallax');
        if (!layer) return;

        section._bgParallaxInit = true;

        var inst = {
            section: section,
            layer:   layer,
            sens:    sens,
            mx: 0, my: 0,   
            tx: 0, ty: 0    
        };
        instances.push(inst);

        

        if (!section.style.perspective) section.style.perspective = '1500px';
        layer.style.willChange     = 'transform';
        layer.style.transformStyle = 'preserve-3d';

        var amp0      = sens / 50;
        var initScale = 1.04 + amp0 * 0.04;
        layer.style.transform = 'translate3d(0,0,0) scale(' + initScale.toFixed(3) + ')';

        section.addEventListener('mousemove', function (e) {
            var rect = section.getBoundingClientRect();
            if (rect.width === 0 || rect.height === 0) return;
            inst.mx = ((e.clientX - rect.left) / rect.width)  * 2 - 1;
            inst.my = ((e.clientY - rect.top)  / rect.height) * 2 - 1;
        }, { passive: true });

        section.addEventListener('mouseleave', function () {
            inst.mx = 0;
            inst.my = 0;
        }, { passive: true });

        if (!rafId) tick();
    }

    function tick() {
        var lerp = 0.08;
        for (var i = 0; i < instances.length; i++) {
            var inst = instances[i];

            if (!inst.section.isConnected) { inst._dead = true; continue; }
            inst.tx += (inst.mx - inst.tx) * lerp;
            inst.ty += (inst.my - inst.ty) * lerp;
            var amp     = inst.sens / 50;           
            var maxDeg  = 8 * amp;
            var rotY    =  inst.tx * maxDeg;
            var rotX    = -inst.ty * maxDeg;
            var scale   = 1.04 + amp * 0.04;
            inst.layer.style.transform =
                'translate3d(0,0,0) ' +
                'rotateX(' + rotX.toFixed(2) + 'deg) ' +
                'rotateY(' + rotY.toFixed(2) + 'deg) ' +
                'scale(' + scale.toFixed(3) + ')';
        }
        if (instances.some(function (i) { return i._dead; })) {
            instances = instances.filter(function (i) { return !i._dead; });
        }
        rafId = requestAnimationFrame(tick);
    }

    function initAll(root) {
        var scope = root || document;
        scope.querySelectorAll('.impulse-section[data-bg-parallax-sens]').forEach(initSection);
    }

    function destroySection(section) {
        if (!section._bgParallaxInit) return;
        section._bgParallaxInit = false;
        instances = instances.filter(function (i) { return i.section !== section; });
        var layer = section.querySelector(':scope > .impulse-section-bg-image-parallax');
        if (layer) {
            layer.style.transform = '';
            layer.style.willChange = '';
        }
    }

    

    

    
    
    function setSens(section, newSens) {
        var s = Math.max(0, Math.min(50, parseInt(newSens, 10) || 0));
        section.setAttribute('data-bg-parallax-sens', String(s));
        var inst = null;
        for (var i = 0; i < instances.length; i++) {
            if (instances[i].section === section) { inst = instances[i]; break; }
        }
        if (s <= 0) {
            if (inst) destroySection(section);
            return;
        }
        if (!inst) {
            initSection(section);
            return;
        }
        inst.sens = s;
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function () { initAll(); });
    } else {
        initAll();
    }

    window.ImpulseBgParallax = {
        init:    initSection,
        initAll: initAll,
        destroy: destroySection,
        setSens: setSens
    };
})();
