import { Host, h } from "@stencil/core";
import { BEM } from "../../utils/bem";
export class Toast {
    constructor() {
        this.toastId = `bal-toast-${toastIds++}`;
        /**
         * The theme type of the toast.
         * Color type primary is deprecated, please use info instead.
         */
        this.color = '';
        /**
         * The duration of the toast in milliseconds.
         */
        this.duration = 0;
        /**
         * If `true` the toast has a cross icon to close the toast.
         */
        this.closable = true;
        /**
         * Content message
         */
        this.message = '';
        /**
         * @internal Handler for on close event
         */
        this.closeHandler = () => void 0;
    }
    async componentWillLoad() {
        if (this.duration > 0) {
            await this.closeIn(this.duration);
        }
    }
    /**
     * Closes the toast after the given duration in ms
     */
    async closeIn(duration) {
        this.timer = setTimeout(() => this.close(), duration);
    }
    /**
     * Closes this toast
     */
    async close() {
        this.element.remove();
        clearTimeout(this.timer);
        this.balClose.emit(this.toastId);
        this.closeHandler();
    }
    render() {
        const block = BEM.block('toast');
        const innerEl = block.element('inner');
        const labelEl = block.element('label');
        const closeEl = block.element('close');
        return (h(Host, { key: '46ad818112b1002e1a48a627eb714dec23aba2a5', id: this.toastId, class: Object.assign({}, block.class()) }, h("div", { key: 'e2db452a14a302ba7ed9ebbeb8a78e801a175998', role: "status", class: Object.assign(Object.assign({}, innerEl.class()), innerEl.modifier(`color-${this.color}`).class(!!this.color)) }, h("span", { key: '41331cb2609ece86bb59951cab9ec61b76b27c33', class: Object.assign({}, labelEl.class()), "data-testid": "bal-toast-label", innerHTML: this.message }, h("slot", { key: '09572e31b7d8bcc5ec8f31e33df31cdca13a7355' })), this.closable !== false ? (h("bal-close", { class: Object.assign({}, closeEl.class()), onClick: () => this.close(), inverted: false, "data-testid": "bal-toast-close" })) : (''))));
    }
    static get is() { return "bal-toast"; }
    static get originalStyleUrls() {
        return {
            "$": ["bal-toast.sass"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bal-toast.css"]
        };
    }
    static get properties() {
        return {
            "color": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalToastColor",
                    "resolved": "\"\" | \"danger\" | \"info\" | \"primary\" | \"success\" | \"warning\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The theme type of the toast.\nColor type primary is deprecated, please use info instead."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "color",
                "defaultValue": "''"
            },
            "duration": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The duration of the toast in milliseconds."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "duration",
                "defaultValue": "0"
            },
            "closable": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the toast has a cross icon to close the toast."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "closable",
                "defaultValue": "true"
            },
            "message": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Content message"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "message",
                "defaultValue": "''"
            },
            "closeHandler": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "() => void",
                    "resolved": "() => void",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "internal",
                            "text": "Handler for on close event"
                        }],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "defaultValue": "() => void 0"
            }
        };
    }
    static get events() {
        return [{
                "method": "balClose",
                "name": "balClose",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when toast is closed"
                },
                "complexType": {
                    "original": "BalEvents.BalToastCloseDetail",
                    "resolved": "string",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "closeIn": {
                "complexType": {
                    "signature": "(duration: number) => Promise<void>",
                    "parameters": [{
                            "name": "duration",
                            "type": "number",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Closes the toast after the given duration in ms",
                    "tags": []
                }
            },
            "close": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Closes this toast",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "element"; }
}
let toastIds = 0;
