import { h, Host } from "@stencil/core";
import { validateProp } from "../../helpers/Validate/props";
import { ToastClass, ToastColors } from "./constants";
import { govHost } from "../../helpers/Dom/template";
import { createID } from "../../utils/string.utils";
import { AttributeObserver } from "../../helpers/Dom/attribute";
import { ariaAttributes } from "../../helpers/Accessibility/attributes";
import { inheritAriaAttributes } from "../../helpers/Accessibility/accessibility";
export class GovToast {
    constructor() {
        /**
         * Style variation of the toast message.
         */
        this.color = "primary";
        /**
         * Toast’s gravity
         */
        this.gravity = "top";
        /**
         * Toast’s position
         */
        this.position = "right";
        /**
         * Toast type
         */
        this.type = "bold";
        /**
         * Number of milliseconds to wait before Toast disappear
         */
        this.time = 0;
        this.inheritedAttributes = {};
        this.h = govHost(this.host);
        this.toastId = createID("GovToast");
    }
    validateColor(newValue) {
        validateProp(ToastColors, newValue, ToastClass.root);
    }
    componentDidLoad() {
        this.attributeObserver = new AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(ariaAttributes);
    }
    componentWillLoad() {
        this.validateColor(this.color);
        this.runTimer();
        this.inheritedAttributes = inheritAriaAttributes(this.host);
    }
    disconnectedCallback() {
        var _a;
        clearTimeout(this.timeout);
        (_a = this.attributeObserver) === null || _a === void 0 ? void 0 : _a.stopObserving();
    }
    onAriaAttributeChange(attributeName, newValue) {
        this.host.removeAttribute(attributeName);
        this.inheritedAttributes = Object.assign(Object.assign({}, this.inheritedAttributes), { [attributeName]: newValue });
    }
    runTimer() {
        if (this.isTimer) {
            this.timeout = setTimeout(() => {
                this.host.classList.add(ToastClass.hidden);
                setTimeout(() => {
                    this.destroy().catch();
                    this.govClose.emit(this.toastId);
                }, 600);
            }, this.time);
        }
    }
    render() {
        return (h(Host, { key: '623b9b85e6b10894429d50c4624bac71808d29ad', class: this.h.classes(ToastClass.root), "data-toast-id": this.toastId, gravity: this.gravity, position: this.position, color: this.color, type: this.type, style: { "--toast-duration": this.time + "ms" } }, this.h.hasSlot("icon") && (h("span", { key: 'ae58b921c0aff0f678176c24d4cf69d05fb6cdab', class: ToastClass.icon }, h("slot", { key: '40d58461b3e16abd1d4cd627057e986548ac63c1', name: "icon" }))), h("div", Object.assign({ key: 'b003f0521f3fb5095b94280e2cf252b2a26ccaed', class: ToastClass.content }, this.inheritedAttributes, { "aria-live": "polite", role: "status" }), h("slot", { key: '4743ada7cc1d31d6ca07871228d3fadc3576982f' })), h("gov-button", { key: '0d4e9bae29391f47389abbc2ada9ca9edaa13b97', size: "s", "aria-label": this.accessibleCloseLabel, color: this.isBold ? this.color : "neutral", type: "base", "on-gov-click": this.onCloseHandler.bind(this), class: ToastClass.close }, this.closeLabel ? this.closeLabel : h("gov-icon", { name: "x-lg", type: "components" })), h("div", { key: 'a8cb56376d11503abe586598f330476a45f59c8e', class: ToastClass.timer })));
    }
    onCloseHandler() {
        this.govClose.emit(this.toastId);
        this.destroy().catch();
    }
    get isBold() {
        return this.type === "bold";
    }
    get isTimer() {
        return Number.isInteger(this.time) && this.time > 0;
    }
    /**
     * Remove toast from dom
     */
    async destroy() {
        clearTimeout(this.timeout);
        const toast = document.querySelector(`[data-toast-id="${this.toastId}"]`);
        if (toast) {
            toast.classList.add(ToastClass.hidden);
            if (typeof toast.remove === "function") {
                toast.remove();
            }
        }
    }
    static get is() { return "gov-toast"; }
    static get properties() {
        return {
            "color": {
                "type": "string",
                "attribute": "color",
                "mutable": false,
                "complexType": {
                    "original": "ToastColorType",
                    "resolved": "\"error\" | \"neutral\" | \"primary\" | \"success\" | \"warning\"",
                    "references": {
                        "ToastColorType": {
                            "location": "import",
                            "path": "./constants",
                            "id": "../../../packages/core/src/components/gov-toast/constants.ts::ToastColorType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Style variation of the toast message."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "\"primary\""
            },
            "gravity": {
                "type": "string",
                "attribute": "gravity",
                "mutable": false,
                "complexType": {
                    "original": "\"top\" | \"bottom\"",
                    "resolved": "\"bottom\" | \"top\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Toast\u2019s gravity"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "\"top\""
            },
            "position": {
                "type": "string",
                "attribute": "position",
                "mutable": false,
                "complexType": {
                    "original": "\"left\" | \"center\" | \"right\"",
                    "resolved": "\"center\" | \"left\" | \"right\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Toast\u2019s position"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "\"right\""
            },
            "type": {
                "type": "string",
                "attribute": "type",
                "mutable": false,
                "complexType": {
                    "original": "ToastTypesType",
                    "resolved": "\"bold\" | \"subtle\"",
                    "references": {
                        "ToastTypesType": {
                            "location": "import",
                            "path": "./constants",
                            "id": "../../../packages/core/src/components/gov-toast/constants.ts::ToastTypesType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Toast type"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "\"bold\""
            },
            "time": {
                "type": "number",
                "attribute": "time",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Number of milliseconds to wait before Toast disappear"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "0"
            },
            "closeLabel": {
                "type": "string",
                "attribute": "close-label",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Closing button texting instead of icons"
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "accessibleCloseLabel": {
                "type": "string",
                "attribute": "accessible-close-label",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Aria label for the closing button. It is mandatory if the alert is closable"
                },
                "getter": false,
                "setter": false,
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "inheritedAttributes": {}
        };
    }
    static get events() {
        return [{
                "method": "govClose",
                "name": "gov-close",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Called when the close button is clicked"
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
    static get methods() {
        return {
            "destroy": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Remove toast from dom",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "host"; }
    static get watchers() {
        return [{
                "propName": "color",
                "methodName": "validateColor"
            }];
    }
}
//# sourceMappingURL=gov-toast.js.map
