import { Host, h } from "@stencil/core";
import { cls, evaluateOverlayIndex, getOverlayContainer } from "../../utils/helpers";
import { Query, Theme } from "../../services/theme";
export class XModal {
    constructor() {
        this.loading = false;
        this.interval = undefined;
        this.size = 'normal';
        this.maxWidth = undefined;
        this.closable = true;
        this.visible = false;
    }
    /**
     * Observe the visible changes
     */
    watchVisible(newValue) {
        newValue && evaluateOverlayIndex(this.el);
        (newValue ? this.onOpen : this.onClose).emit(this.el);
    }
    /**
     * Open the modal window
     */
    async open() {
        this.visible = true;
        if (this.interval && this.interval > 0) {
            this.timer = setTimeout(() => {
                this.close();
            }, this.interval * 1000);
        }
    }
    /**
     * Close the modal window
     */
    async close() {
        if (this.timer) {
            clearTimeout(this.timer);
        }
        this.visible = false;
        Theme.validateModals();
    }
    /**
     * On component did loaded hook
     */
    componentDidLoad() {
        const container = getOverlayContainer();
        const ref = this.el.getAttribute('ref');
        if (ref && Query(ref, container)) {
            Query(ref, container).remove();
        }
        container.appendChild(this.el);
    }
    /**
     * On click modal container handler
     *
     * @param e
     * @returns
     */
    onClickContainer(e) {
        if (this.loading || !this.closable) {
            return;
        }
        if ((e.target.classList.contains('modal') || e.target.classList.contains('modal-container'))) {
            this.close();
        }
    }
    /**
     * Render element
     */
    render() {
        return h(Host, { key: '39f3bf2078528b53f6f3c447606050e2cf4444bb', role: "modal", class: cls('modal', { 'modal-show': this.visible, 'modal-closable': this.closable, 'modal-loading': this.loading }), onClick: this.onClickContainer.bind(this) }, h("div", { key: '52dc025064e30bc96f8de4797d4417eae3e304a6', class: "modal-container" }, this.loading && h("div", { key: 'c0276a0dfb58a5c5848881037ec9b1fd01ad0313' }, h("div", { key: '1ee10a8e4db01da0f8ec350520003a61016cdc1b', class: "modal-loading" }, h("span", { key: '728d3337dedcfcf61c276d8f32aaa5639de015a3' }))), h("div", { key: '084add3d0a6a5e59c97e83ff8593328e550d020d', class: cls('modal-slot', `modal-${this.size}`), style: { maxWidth: this.maxWidth } }, h("slot", { key: '992b81a264e71271efa83f6c9983a89c0b9e3311' }))), this.interval > 0 && this.visible && h("span", { key: '143151d5fe50640ba66fd34fc318ddc89746f303', class: "modal-interval-indicator" }, h("span", { key: '80bc1633fbff703a9efa753292176a121ad5caf7', style: { animationDuration: this.interval + 's' } })));
    }
    ;
    static get is() { return "x-modal"; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "loading": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element show loading style"
                },
                "attribute": "loading",
                "reflect": false,
                "defaultValue": "false"
            },
            "interval": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element interval time in seconds to auto-close"
                },
                "attribute": "interval",
                "reflect": false
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "ModalSizeType",
                    "resolved": "\"auto\" | \"big\" | \"full\" | \"large\" | \"medium\" | \"normal\" | \"regular\" | \"small\"",
                    "references": {
                        "ModalSizeType": {
                            "location": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-modal/components.tsx",
                            "id": "src/components/x-modal/components.tsx::ModalSizeType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element size name"
                },
                "attribute": "size",
                "reflect": false,
                "defaultValue": "'normal'"
            },
            "maxWidth": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element max width"
                },
                "attribute": "max-width",
                "reflect": false
            },
            "closable": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element closable on click outside"
                },
                "attribute": "closable",
                "reflect": false,
                "defaultValue": "true"
            }
        };
    }
    static get states() {
        return {
            "visible": {}
        };
    }
    static get events() {
        return [{
                "method": "onOpen",
                "name": "open",
                "bubbles": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On open modal emitter event"
                },
                "complexType": {
                    "original": "HostElement",
                    "resolved": "HostElement",
                    "references": {
                        "HostElement": {
                            "location": "import",
                            "path": "@stencil/core/internal",
                            "id": "node_modules::HostElement"
                        }
                    }
                }
            }, {
                "method": "onClose",
                "name": "close",
                "bubbles": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On close modal emitter event"
                },
                "complexType": {
                    "original": "HostElement",
                    "resolved": "HostElement",
                    "references": {
                        "HostElement": {
                            "location": "import",
                            "path": "@stencil/core/internal",
                            "id": "node_modules::HostElement"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "open": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Open the modal window",
                    "tags": []
                }
            },
            "close": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Close the modal window",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "visible",
                "methodName": "watchVisible"
            }];
    }
}
