'use strict';

var index = require('./index-BEF5cZxd.js');
var props = require('./props-CPENhBnM.js');
var colors = require('./colors-DgrrxjsX.js');
var utils = require('./utils-NnC78QR3.js');
var template = require('./template-Cj6PcLmR.js');
var string_utils = require('./string.utils-BG4pUfEi.js');
var attribute = require('./attribute-CH1-zDDl.js');
var accessibility = require('./accessibility-BQJmIVWV.js');
require('./gov.log-C5EH15b_.js');

const ToastColors = utils.pick(colors.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) {
        index.registerInstance(this, hostRef);
        this.govClose = index.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 = template.govHost(this.host);
        this.toastId = string_utils.createID("GovToast");
    }
    validateColor(newValue) {
        props.validateProp(ToastColors, newValue, ToastClass.root);
    }
    componentDidLoad() {
        this.attributeObserver = new attribute.AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(attribute.ariaAttributes);
    }
    componentWillLoad() {
        this.validateColor(this.color);
        this.runTimer();
        this.inheritedAttributes = accessibility.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 (index.h(index.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") && (index.h("span", { key: 'ae58b921c0aff0f678176c24d4cf69d05fb6cdab', class: ToastClass.icon }, index.h("slot", { key: '40d58461b3e16abd1d4cd627057e986548ac63c1', name: "icon" }))), index.h("div", Object.assign({ key: 'b003f0521f3fb5095b94280e2cf252b2a26ccaed', class: ToastClass.content }, this.inheritedAttributes, { "aria-live": "polite", role: "status" }), index.h("slot", { key: '4743ada7cc1d31d6ca07871228d3fadc3576982f' })), index.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 : index.h("gov-icon", { name: "x-lg", type: "components" })), index.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 index.getElement(this); }
    static get watchers() { return {
        "color": ["validateColor"]
    }; }
};

exports.gov_toast = GovToast;
//# sourceMappingURL=gov-toast.entry.cjs.js.map

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