import { createPopper } from "@popperjs/core";
import { h, Host } from "@stencil/core";
/**
 * Display an icon button that triggers a drop-down panel that can display
 * arbitrary child content, such as metadata, forms, or other controls.
 *
 * ```ts
 *   <verdocs-button-panel .icon=${icon}>
 *     <h6>Field Settings</h6>
 *     <form>
 *     <p>
 *       <label>Field Name</label>
 *       <input type="text" placeholder="Field Name..." />
 *     </p>
 *     </form>
 *   </verdocs-button-panel>
 * ```
 */
export class VerdocsButtonPanel {
    constructor() {
        /**
         * SVG icon to display
         */
        this.icon = '';
        // Not marked as @State because it's not the thing that controls our rendering
        this.showing = false;
    }
    componentDidRender() {
        document.body.appendChild(this.panelEl);
        if (this.popperInstance) {
            this.popperInstance.destroy();
        }
        this.popperInstance = createPopper(this.iconEl, this.panelEl, {
        // placement: 'top-end',
        // modifiers: [{name: 'offset', options: {offset: [0, 10]}}],
        });
    }
    disconnectedCallback() {
        if (this.popperInstance) {
            this.popperInstance.destroy();
            this.popperInstance = null;
        }
        if (this.panelEl) {
            this.panelEl.remove();
        }
    }
    async showPanel() {
        var _a, _b;
        (_a = this.panelEl) === null || _a === void 0 ? void 0 : _a.setAttribute('data-show', '');
        (_b = this.popperInstance) === null || _b === void 0 ? void 0 : _b.update().catch(() => { });
        this.showing = true;
        this.hiderEl = document.createElement('div');
        this.hiderEl.id = 'verdocs-button-panel-hider';
        this.hiderEl.style.zIndex = '100';
        this.hiderEl.style.position = 'absolute';
        this.hiderEl.style.top = '0px';
        this.hiderEl.style.left = '0px';
        this.hiderEl.style.right = '0px';
        this.hiderEl.style.bottom = '0px';
        this.hiderEl.onclick = (e) => {
            e.stopPropagation();
            this.toggle();
        };
        document.body.appendChild(this.hiderEl);
    }
    async hidePanel() {
        var _a;
        Array.from(document.getElementsByClassName('verdocs-button-panel-content')).forEach(el => {
            el.removeAttribute('data-show');
        });
        (_a = document.getElementById('verdocs-button-panel-hider')) === null || _a === void 0 ? void 0 : _a.remove();
        this.showing = false;
    }
    async toggle() {
        if (this.showing) {
            await this.hidePanel();
        }
        else {
            await this.showPanel();
        }
    }
    render() {
        return (h(Host, { key: 'ac59044fd14cb2998684f1b8f1c54aedec37f256' }, h("div", { key: 'e62fb7d53428ba82bae2622aede06e15db19fec9', class: "icon", innerHTML: this.icon, onClick: (e) => {
                e.stopPropagation();
                return this.toggle();
            }, ref: el => (this.iconEl = el) }), h("div", { key: '4cccc4f6d5f3294074f6d1e41ce768a023d6fdb0', role: "tooltip", class: "verdocs-button-panel-content", "data-popper-placement": "bottom", ref: el => (this.panelEl = el) }, h("div", { key: 'e02fba4c1cee732b08d01b5729f1edd02c9262e5', "data-popper-arrow": "true", class: "arrow" }), h("slot", { key: '323f842de2f33348bcbcf67b1c576dce2275dcf9' }))));
    }
    static get is() { return "verdocs-button-panel"; }
    static get originalStyleUrls() {
        return {
            "$": ["verdocs-button-panel.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["verdocs-button-panel.css"]
        };
    }
    static get properties() {
        return {
            "icon": {
                "type": "string",
                "attribute": "icon",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "SVG icon to display"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "''"
            }
        };
    }
    static get methods() {
        return {
            "showPanel": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "hidePanel": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "toggle": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
}
//# sourceMappingURL=verdocs-button-panel.js.map
