
(function () {
    function wireRow(li, mode) {
        if (li._hhMode === mode) return;
        li._hhMode = mode;
        var iconSpan = li.querySelector(':scope > span:first-child, :scope > a > span:first-child');
        var textSpan = li.querySelector(':scope > span:nth-of-type(2), :scope > a > span:nth-of-type(2)');
        var layers = [];
        if (iconSpan) layers.push({ el: iconSpan, z: 28, travel: 12 });
        if (textSpan) layers.push({ el: textSpan, z: 10, travel: 4 });
        var hovered = false, tx = 0, ty = 0, cx = 0, cy = 0, raf = null;

        function apply() {
            cx += (tx - cx) * 0.18;
            cy += (ty - cy) * 0.18;
            if (mode === 'tilt') {
                var rx = hovered ? -cy * 6 : 0;
                var ry = hovered ?  cx * 8 : 0;
                var tz = hovered ? 10 : 0;
                li.style.transform = 'perspective(700px) rotateX(' + rx.toFixed(2) + 'deg) rotateY(' + ry.toFixed(2) + 'deg) translateZ(' + tz + 'px)';
            } else {
                layers.forEach(function (p) {
                    var dx = cx * p.travel;
                    var dy = cy * p.travel;
                    var z  = hovered ? p.z : 0;
                    p.el.style.transform = 'translate3d(' + dx.toFixed(2) + 'px,' + dy.toFixed(2) + 'px,' + z + 'px)';
                });
            }
            if (hovered || Math.abs(cx - tx) > 0.01 || Math.abs(cy - ty) > 0.01) {
                raf = requestAnimationFrame(apply);
            } else {
                raf = null;
            }
        }

        function resetTransition(on) {
            var val = on ? 'none' : 'transform 0.45s cubic-bezier(0.4, 0, 0.2, 1)';
            if (mode === 'tilt') { li.style.transition = val; }
            else layers.forEach(function (p) { p.el.style.transition = val; });
        }

        li.addEventListener('mouseenter', function () {
            hovered = true;
            resetTransition(true);
            if (!raf) raf = requestAnimationFrame(apply);
        });
        li.addEventListener('mousemove', function (e) {
            var r = li.getBoundingClientRect();
            if (!r.width || !r.height) return;
            tx = ((e.clientX - r.left) / r.width  - 0.5) * 2;
            ty = ((e.clientY - r.top)  / r.height - 0.5) * 2;
        });
        li.addEventListener('mouseleave', function () {
            hovered = false;
            tx = 0; ty = 0;
            resetTransition(false);
            if (!raf) raf = requestAnimationFrame(apply);
        });
    }

    function wireList(ul) {
        var mode = null;
        if (ul.classList.contains('impulse-hh-parallax')) mode = 'parallax';
        else if (ul.classList.contains('impulse-hh-tilt')) mode = 'tilt';
        if (!mode) return;
        ul.querySelectorAll(':scope > li').forEach(function (li) { wireRow(li, mode); });
    }

    function scan(root) {
        (root || document).querySelectorAll('.impulse-table-hover').forEach(wireList);
    }

    function init() {
        scan(document);
        var mo = new MutationObserver(function (list) {
            list.forEach(function (m) {
                m.addedNodes && m.addedNodes.forEach(function (n) {
                    if (n.nodeType !== 1) return;
                    if (n.classList && n.classList.contains('impulse-table-hover')) wireList(n);
                    n.querySelectorAll && n.querySelectorAll('.impulse-table-hover').forEach(wireList);
                });
            });
        });
        mo.observe(document.body, { childList: true, subtree: true });
    }

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

    window.ImpulseIconListHover = { wire: wireList, scan: scan };
})();
