// Tailwind content scanning requires complete class names (no template literals).
const POSITION_CLASSES = {
    top: 'btu-tooltip-top',
    bottom: 'btu-tooltip-bottom',
    left: 'btu-tooltip-left',
    right: 'btu-tooltip-right',
};
const OFFSET_CLASSES = {
    sm: 'btu-tooltip-offset-sm',
    md: 'btu-tooltip-offset-md',
    lg: 'btu-tooltip-offset-lg',
    xl: 'btu-tooltip-offset-xl',
};
/**
 * Reactive Controller that attaches tooltips to sub-elements within a LitElement.
 * Reuses the same CSS classes, Popover API, and CSS anchor positioning as TooltipMixin.
 */
export class TooltipController {
    constructor(host, options) {
        this._tooltipEl = null;
        this._targetEl = null;
        this._anchorName = `--btu-trigger-${crypto.randomUUID()}`;
        this._showTimer = null;
        this._ac = null;
        this._host = host;
        this._options = options;
        host.addController(this);
    }
    hostUpdated() {
        const target = this._host.querySelector(this._options.target);
        if (target && target !== this._targetEl) {
            this._detach();
            this._targetEl = target;
            this._attach();
        }
        else if (!target && this._targetEl) {
            this._detach();
        }
        if (this._tooltipEl) {
            const text = this._options.text();
            if (this._tooltipEl.textContent !== text) {
                this._tooltipEl.textContent = text;
            }
        }
    }
    hostDisconnected() {
        this._detach();
    }
    _attach() {
        if (!this._targetEl)
            return;
        const { position = 'top', offset, noArrow, text, delay = 300 } = this._options;
        const classes = ['btu-tooltip', POSITION_CLASSES[position]];
        if (offset)
            classes.push(OFFSET_CLASSES[offset]);
        if (noArrow)
            classes.push('btu-tooltip-no-arrow');
        const el = document.createElement('div');
        el.className = classes.join(' ');
        el.setAttribute('role', 'tooltip');
        el.setAttribute('data-tooltip-internal', '');
        el.setAttribute('popover', 'manual');
        el.style.setProperty('position-anchor', this._anchorName);
        el.textContent = text();
        this._tooltipEl = el;
        this._targetEl.style.setProperty('anchor-name', this._anchorName);
        this._host.appendChild(el);
        const ac = new AbortController();
        this._ac = ac;
        const o = { signal: ac.signal };
        this._targetEl.addEventListener('mouseenter', () => {
            this._clearShowTimer();
            this._showTimer = setTimeout(() => this._show(), delay);
        }, o);
        this._targetEl.addEventListener('mouseleave', () => this._hide(), o);
        this._targetEl.addEventListener('focusin', () => this._show(), o);
        this._targetEl.addEventListener('focusout', () => this._hide(), o);
        this._targetEl.addEventListener('click', () => this._hide(), o);
        document.addEventListener('keydown', e => {
            if (e.key === 'Escape')
                this._hide();
        }, o);
    }
    _detach() {
        this._clearShowTimer();
        this._ac?.abort();
        this._ac = null;
        if (this._targetEl) {
            ;
            this._targetEl.style.removeProperty('anchor-name');
            this._targetEl = null;
        }
        if (this._tooltipEl) {
            try {
                this._tooltipEl.hidePopover();
            }
            catch {
                /* already hidden */
            }
            this._tooltipEl.remove();
            this._tooltipEl = null;
        }
    }
    _show() {
        this._clearShowTimer();
        if (!this._tooltipEl || this._tooltipEl.matches(':popover-open'))
            return;
        try {
            this._tooltipEl.showPopover();
        }
        catch {
            /* invalid state */
        }
    }
    _hide() {
        this._clearShowTimer();
        if (!this._tooltipEl || !this._tooltipEl.matches(':popover-open'))
            return;
        try {
            this._tooltipEl.hidePopover();
        }
        catch {
            /* invalid state */
        }
    }
    _clearShowTimer() {
        if (this._showTimer !== null) {
            clearTimeout(this._showTimer);
            this._showTimer = null;
        }
    }
}
//# sourceMappingURL=TooltipController.js.map