import { Host, h } from "@stencil/core";
import { createPopper } from "@popperjs/core";
import { getTooltipElement, tinyMDtoHTML } from "../../utils/helpers";
const popperDefaultConfig = {
    placement: 'auto',
    modifiers: [
        {
            name: 'offset',
            options: {
                offset: [0, 4],
            },
        },
    ],
};
const showEvents = ['mouseenter'];
const hideEvents = ['mouseleave', 'blur'];
export class XTooltip {
    constructor() {
        this.content = undefined;
        this.align = 'top';
    }
    /**
     * 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));
        });
    }
    /**
     * On component did destroyed hook
     */
    disconectedCallback() {
        showEvents.forEach(event => {
            this.el.removeEventListener(event, this.showHandler);
        });
        hideEvents.forEach(event => {
            this.el.removeEventListener(event, this.hideHandler);
        });
        if (this.popper) {
            this.popper.destroy();
        }
    }
    /**
     * Show tooltip overlay popper
     */
    showHandler() {
        this.tooltip.setAttribute('data-show', '');
        this.tooltip.querySelector('span').innerHTML = tinyMDtoHTML(this.content) || '';
        this.popper = createPopper(this.el, this.tooltip, Object.assign(Object.assign({}, popperDefaultConfig), { placement: this.align }));
    }
    /**
     * Hide tooltip overlay popper
     */
    hideHandler() {
        this.tooltip.removeAttribute('data-show');
        this.tooltip.querySelector('span').innerHTML = '';
        this.popper.destroy();
        this.popper = null;
    }
    /**
     * Render element
     */
    render() {
        return h(Host, { key: 'b50b8a5bdb305699d7553b82ac2156ca7caa373d', "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"; }
}
