/**
 * Tooltips globales: sustituye el tooltip nativo del navegador por .antara-tooltip
 * (negro, texto blanco, bordes redondeados). Soporta title y data-tooltip.
 */
(function () {
    'use strict';

    var SHOW_DELAY = 400;
    var GAP = 8;
    var tipEl = null;
    var showTimer = null;
    var hideTimer = null;
    var currentEl = null;

    function createTip() {
        if (tipEl) return tipEl;
        tipEl = document.createElement('div');
        tipEl.className = 'antara-tooltip';
        tipEl.id = 'antara-tooltip';
        tipEl.setAttribute('role', 'tooltip');
        document.body.appendChild(tipEl);
        return tipEl;
    }

    function getText(el) {
        if (!el || el.nodeType !== 1) return '';
        var dt = el.getAttribute('data-tooltip');
        if (dt && String(dt).trim()) return String(dt).trim();
        var title = el.getAttribute('title');
        if (title && String(title).trim()) return String(title).trim();
        return '';
    }

    function isDisabled(el) {
        return el.matches(':disabled') || el.getAttribute('aria-disabled') === 'true';
    }

    function findTarget(start) {
        var el = start;
        while (el && el !== document.documentElement) {
            if (el.nodeType === 1 && el.id !== 'antara-tooltip' && !el.classList.contains('no-tooltip') && el.getAttribute('data-tooltip') !== 'off') {
                if (!isDisabled(el) && getText(el)) return el;
            }
            el = el.parentElement;
        }
        return null;
    }

    function suppressNativeTitle(el) {
        if (!el.hasAttribute('title')) return;
        el.setAttribute('data-antara-title-backup', el.getAttribute('title'));
        el.removeAttribute('title');
    }

    function restoreNativeTitle(el) {
        if (!el) return;
        var backup = el.getAttribute('data-antara-title-backup');
        if (backup !== null) {
            el.setAttribute('title', backup);
            el.removeAttribute('data-antara-title-backup');
        }
    }

    function hide() {
        clearTimeout(showTimer);
        clearTimeout(hideTimer);
        showTimer = null;
        hideTimer = null;
        if (tipEl) {
            tipEl.classList.remove('is-visible');
            tipEl.textContent = '';
        }
        if (currentEl) {
            restoreNativeTitle(currentEl);
            currentEl = null;
        }
    }

    function position(el) {
        var tip = createTip();
        var rect = el.getBoundingClientRect();
        tip.style.left = '0';
        tip.style.top = '0';
        var tw = tip.offsetWidth;
        var th = tip.offsetHeight;
        var left = rect.left + rect.width / 2 - tw / 2;
        var top = rect.bottom + GAP;
        if (top + th > window.innerHeight - 4) {
            top = rect.top - th - GAP;
        }
        left = Math.max(4, Math.min(left, window.innerWidth - tw - 4));
        top = Math.max(4, Math.min(top, window.innerHeight - th - 4));
        tip.style.left = Math.round(left) + 'px';
        tip.style.top = Math.round(top) + 'px';
    }

    function show(el) {
        var text = getText(el);
        if (!text) return;
        hide();
        currentEl = el;
        suppressNativeTitle(el);
        var tip = createTip();
        tip.textContent = text;
        tip.classList.add('is-visible');
        position(el);
    }

    function scheduleShow(el) {
        clearTimeout(showTimer);
        clearTimeout(hideTimer);
        hideTimer = null;
        if (currentEl && currentEl !== el) hide();
        showTimer = setTimeout(function () {
            showTimer = null;
            show(el);
        }, SHOW_DELAY);
    }

    function containsTarget(el, related) {
        if (!el || !related) return false;
        return el === related || el.contains(related);
    }

    function onPointerOver(e) {
        var target = findTarget(e.target);
        if (!target) {
            if (!containsTarget(currentEl, e.target)) hide();
            return;
        }
        if (currentEl === target && tipEl && tipEl.classList.contains('is-visible')) return;
        scheduleShow(target);
    }

    function onPointerOut(e) {
        var target = findTarget(e.target);
        if (target && containsTarget(target, e.relatedTarget)) return;
        if (currentEl && containsTarget(currentEl, e.relatedTarget)) return;
        clearTimeout(showTimer);
        showTimer = null;
        hideTimer = setTimeout(hide, 80);
    }

    function onFocusIn(e) {
        var target = findTarget(e.target);
        if (!target) return;
        scheduleShow(target);
    }

    function onFocusOut(e) {
        if (currentEl && containsTarget(currentEl, e.relatedTarget)) return;
        clearTimeout(showTimer);
        showTimer = null;
        hideTimer = setTimeout(hide, 80);
    }

    function init() {
        if (!document.body) return;
        document.addEventListener('mouseover', onPointerOver, true);
        document.addEventListener('mouseout', onPointerOut, true);
        document.addEventListener('focusin', onFocusIn, true);
        document.addEventListener('focusout', onFocusOut, true);
        window.addEventListener('scroll', hide, true);
        window.addEventListener('resize', hide);
        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape') hide();
        });
    }

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