'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-b55be5aa.js');
const helpers = require('./helpers-a6a0ad60.js');
const floatingUi_dom_esm = require('./floating-ui.dom.esm-ae1af6c6.js');

const styleCss = "x-dropdown{display:inherit;text-align:inherit}x-dropdown .dropdown-container{display:block;width:-webkit-max-content;width:-moz-max-content;width:max-content;top:0;left:0;position:absolute;z-index:10000;visibility:hidden;opacity:0;-webkit-transition:opacity 0.3s;transition:opacity 0.3s;pointer-events:none}x-dropdown .dropdown-container.dropdown-show{pointer-events:initial;opacity:1;visibility:visible}";
const XDropdownStyle0 = styleCss;

const XDropdown = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.show = false;
        this.align = 'bottom';
        this.hover = false;
        this.syncWidth = false;
        this.offset = 0;
    }
    /**
     * Update setting on align change
     */
    watchAlignAndShow() {
        this.updateDropdownContainer();
    }
    /**
     * On element is mounted
     */
    componentDidLoad() {
        this.container = this.el.querySelector('.dropdown-container');
        this.updateDropdownContainer();
        this.el.addEventListener('mouseenter', this.onMouseEnterOnContainer.bind(this));
        this.el.addEventListener('mouseleave', this.onMouseLeaveOnContainer.bind(this));
    }
    /**
     * On component did destroyed hook
     */
    disconectedCallback() {
        if (this.el) {
            this.el.removeEventListener('mouseenter', this.onMouseEnterOnContainer);
            this.el.removeEventListener('mouseleave', this.onMouseLeaveOnContainer);
        }
    }
    /**
     * Event triggered on mouse enter hover container
     */
    onMouseEnterOnContainer() {
        if (this.hover) {
            this.show = true;
        }
    }
    /**
     * Event triggered on mouse leave hover container
     */
    onMouseLeaveOnContainer() {
        if (this.hover) {
            this.show = false;
        }
    }
    /**
     * Update the instance or create
     */
    updateDropdownContainer() {
        if (this.syncWidth) {
            this.container.style.maxWidth = `${this.el.offsetWidth}px`;
            this.container.style.width = '100%';
        }
        else {
            this.container.style.maxWidth = 'auto';
            this.container.style.width = 'auto';
        }
        setTimeout(() => {
            floatingUi_dom_esm.computePosition(this.el, this.container, {
                placement: this.align,
                middleware: [floatingUi_dom_esm.flip(), floatingUi_dom_esm.shift({ padding: 10 }), floatingUi_dom_esm.offset(this.offset)],
            }).then(({ x, y }) => {
                this.container.style.left = `${x}px`;
                this.container.style.top = `${y}px`;
            });
        }, 50);
    }
    /**
     * Show tooltip overlay
     */
    async display(show) {
        this.show = show;
    }
    /**
     * Render element
     */
    render() {
        return index.h(index.Host, { key: '48df29d08ae8077798b586c71443872e1894f0bf' }, index.h("slot", { key: '4c623391741dfad19c2e61e20a57327f0ca13c92' }), index.h("div", { key: '2d058e90c91481f871c41354c964ce87975e7732', class: helpers.cls('dropdown-container', { 'dropdown-show': this.show, 'dropdown-hover': this.hover }) }, index.h("slot", { key: '71cc31936e0aefa2fd1570ede53838f1278139ff', name: "dropdown" })));
    }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "align": ["watchAlignAndShow"],
        "show": ["watchAlignAndShow"]
    }; }
};
XDropdown.style = XDropdownStyle0;

exports.x_dropdown = XDropdown;
