import { Host, h } from "@stencil/core";
import { getTooltipElement, tinyMDtoHTML } from "../../utils/helpers";
import { computePosition, offset, shift, arrow, autoPlacement, flip } from "@floating-ui/dom";
const showEvents = ['mouseenter', 'touchstart'];
const hideEvents = ['mouseleave', 'blur', 'touchend'];
export class XTooltip {
    constructor() {
        this.content = undefined;
        this.align = 'top';
    }
    onPropChange() {
        const isVisible = XTooltip.activeElements.has(this.el);
        if (isVisible) {
            this.hideHandler();
            setTimeout(() => {
                this.showHandler();
            }, 10);
        }
    }
    /**
     * On component did loaded hook
     */
    componentDidLoad() {
        this.tooltip = getTooltipElement();
        showEvents.forEach(event => {
            this.el.addEventListener(event, this.showHandler.bind(this));
        });
        hideEvents.forEach(event => {
            this.el.addEventListener(event, this.hideHandler.bind(this));
        });
        window.addEventListener('scroll', this.hideHandler, true);
        window.addEventListener('resize', this.hideHandler, true);
        document.addEventListener('visibilitychange', this.hideHandler, true);
        document.addEventListener('touchmove', this.hideHandler, true);
    }
    /**
     * On component did destroyed hook
     */
    disconectedCallback() {
        this.hideHandler();
        XTooltip.activeElements.delete(this.el);
        showEvents.forEach(event => {
            this.el.removeEventListener(event, this.showHandler);
        });
        hideEvents.forEach(event => {
            this.el.removeEventListener(event, this.hideHandler);
        });
        window.removeEventListener('scroll', this.hideHandler, true);
        window.removeEventListener('resize', this.hideHandler, true);
        document.removeEventListener('visibilitychange', this.hideHandler, true);
        document.removeEventListener('touchmove', this.hideHandler, true);
    }
    /**
     * Show tooltip overlay
     */
    showHandler() {
        XTooltip.activeElements.add(this.el);
        this.tooltip.setAttribute('data-show', '');
        this.tooltip.querySelector('span').innerHTML = tinyMDtoHTML(this.content) || '';
        const arrowEl = this.tooltip.querySelector('.tooltip-arrow');
        computePosition(this.el, this.tooltip, {
            placement: this.align,
            middleware: [this.align == 'auto' ? autoPlacement() : flip(), shift({ padding: 10 }), offset(4), arrow({ element: arrowEl })],
        }).then(({ x, y, middlewareData, placement }) => {
            this.tooltip.style.left = `${x}px`;
            this.tooltip.style.top = `${y}px`;
            if (middlewareData.arrow) {
                const { x: arrowX, y: arrowY } = middlewareData.arrow;
                const staticSide = {
                    top: 'bottom',
                    right: 'left',
                    bottom: 'top',
                    left: 'right',
                }[placement.split('-')[0]];
                Object.assign(arrowEl.style, {
                    left: arrowX != null ? `${arrowX}px` : '',
                    top: arrowY != null ? `${arrowY}px` : '',
                    right: '',
                    bottom: '',
                    [staticSide]: '-4px',
                });
            }
        });
    }
    /**
     * Hide tooltip overlay
     */
    hideHandler() {
        XTooltip.activeElements.delete(this.el);
        if (XTooltip.activeElements.size === 0) {
            this.tooltip.setAttribute('data-show', '');
            this.tooltip.removeAttribute('data-show');
            this.tooltip.querySelector('span').innerHTML = '';
        }
    }
    /**
     * Render element
     */
    render() {
        return h(Host, { key: 'cf277a7fdd90eb2a0f05a8e92609fae4ff2ae7fa', "aria-label": this.content });
    }
    static get is() { return "x-tooltip"; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "content": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element content text to show in tooltip"
                },
                "attribute": "content",
                "reflect": false
            },
            "align": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "TooltipAlignType",
                    "resolved": "\"auto\" | \"bottom\" | \"left\" | \"right\" | \"top\"",
                    "references": {
                        "TooltipAlignType": {
                            "location": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-tooltip/component.tsx",
                            "id": "src/components/x-tooltip/component.tsx::TooltipAlignType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Tooltip aligment to show over element"
                },
                "attribute": "align",
                "reflect": false,
                "defaultValue": "'top'"
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "content",
                "methodName": "onPropChange"
            }, {
                "propName": "align",
                "methodName": "onPropChange"
            }];
    }
}
/**
 * All active elements
 */
XTooltip.activeElements = new Set();
