import { Host, h } from "@stencil/core";
import { cls, getOverlayContainer } from "../../utils/helpers";
import { Query, Theme } from "../../services/theme";
export class XModal {
    constructor() {
        this.loading = false;
        this.interval = undefined;
        this.size = 'normal';
        this.closable = true;
        this.visible = false;
    }
    onVisisbleValueChange(newValue) {
        (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: '5b7c64d8dc34f6499268c6a85786cdc564209b67', role: "modal", class: cls('modal', { 'modal-show': this.visible, 'modal-closable': this.closable }), onClick: this.onClickContainer.bind(this) }, h("div", { key: '0006a3db1af68af5e036e30f94df88cc5962c9f4', class: "modal-container" }, this.loading ? h("div", null, h("div", { class: "d-none" }, h("slot", null)), h("div", { class: "modal-loading" }, h("span", null))) :
            h("div", { class: cls(`modal-${this.size}`) }, h("slot", null))), this.interval > 0 && this.visible && h("span", { key: '40af83340c36433c44c45578204799936f3b531a', class: "modal-interval-indicator" }, h("span", { key: '91cbe5497cd94d76c2c70725a7d5af202ad1b7d6', 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": "\"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'"
            },
            "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": true,
                "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": true,
                "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": "onVisisbleValueChange"
            }];
    }
}
