import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-ClISz4NF.js';
import { v as validateProp } from './props-CJZgTDla.js';
import { C as Colors } from './colors-U40vKHrs.js';
import { p as pick } from './utils-CQvUntGY.js';
import { g as govHost } from './template-CoD1xawt.js';
import { c as createID } from './string.utils-vN03FcXN.js';
import { A as AttributeObserver, a as ariaAttributes } from './attribute-Bz_RzYFb.js';
import { i as inheritAriaAttributes } from './accessibility-wqNV8djF.js';
import './gov.log-BtqD9K8d.js';

const ToastColors = pick(Colors, ['PRIMARY', 'NEUTRAL', 'ERROR', 'SUCCESS', 'WARNING']);
var ToastType;
(function (ToastType) {
    ToastType["BOLD"] = "bold";
    ToastType["SUBTLE"] = "subtle";
})(ToastType || (ToastType = {}));
const ToastClass = {
    root: 'gov-toast',
    hidden: 'gov-toast--hidden',
    content: 'gov-toast__content',
    icon: 'gov-toast__icon',
    close: 'gov-toast__close',
    timer: 'gov-toast__timer',
};

const GovToast = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.govClose = createEvent(this, "gov-close");
        /**
         * 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();
            }
        }
    }
    get host() { return getElement(this); }
    static get watchers() { return {
        "color": ["validateColor"]
    }; }
};

export { GovToast as gov_toast };
//# sourceMappingURL=gov-toast.entry.js.map

//# sourceMappingURL=gov-toast.entry.js.map