import '../snackbar/ui-snackbar.js';
/**
 * A class that manages user notifications via a snackbar (toast messages).
 */
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class SnackNotifications {
    static current;
    static stack = [];
    static notify(message, timeoutOrInit) {
        const snack = document.createElement('ui-snackbar');
        snack.innerText = message;
        let configInit;
        if (typeof timeoutOrInit === 'number') {
            snack.timeout = timeoutOrInit;
        }
        else if (timeoutOrInit) {
            const cnf = timeoutOrInit;
            configInit = cnf;
            if (typeof cnf.timeout === 'number') {
                snack.timeout = cnf.timeout;
            }
            if (typeof cnf.persistent === 'boolean') {
                snack.persistent = cnf.persistent;
            }
            if (typeof cnf.close === 'boolean') {
                snack.close = cnf.close;
            }
            if (typeof cnf.cancellable === 'boolean') {
                snack.cancellable = cnf.cancellable;
            }
            if (cnf.actionLabel && typeof cnf.actionCallback === 'function') {
                snack.action = cnf.actionLabel;
            }
        }
        this.stack.push({
            element: snack,
            init: configInit,
        });
        document.body.appendChild(snack);
        snack.addEventListener('action', this.actionHandler.bind(this));
        snack.addEventListener('open', this.openHandler.bind(this));
        snack.addEventListener('dismiss', this.dismissHandler.bind(this));
        setTimeout(() => {
            this.schedule();
        });
        return snack;
    }
    static schedule() {
        if (this.current) {
            return;
        }
        const { stack = [] } = this;
        const item = stack.shift();
        if (!item) {
            return;
        }
        this.current = item;
        item.element.open = true;
    }
    static actionHandler(e) {
        const target = e.target;
        const info = SnackNotifications.current;
        if (!info) {
            return;
        }
        if (target !== info.element) {
            return;
        }
        if (info.init && info.init.actionCallback) {
            info.init.actionCallback();
        }
    }
    static openHandler(e) {
        const target = e.target;
        if (target.open) {
            return;
        }
        const info = SnackNotifications.current;
        if (!info) {
            return;
        }
        if (target !== info.element) {
            return;
        }
        target.parentElement?.removeChild(target);
        if (info.init && info.init.closed) {
            info.init.closed();
        }
        SnackNotifications.current = undefined;
        SnackNotifications.schedule();
    }
    static dismissHandler(e) {
        const target = e.target;
        const info = SnackNotifications.current;
        if (!info) {
            return;
        }
        if (target !== info.element) {
            return;
        }
        target.parentElement?.removeChild(target);
        SnackNotifications.current = undefined;
        SnackNotifications.schedule();
    }
    static cancel(snack) {
        if (this.current?.element === snack) {
            if (snack.open) {
                snack.open = false;
            }
            else {
                snack.parentElement?.removeChild(snack);
                this.current = undefined;
                SnackNotifications.schedule();
            }
        }
        else {
            const index = this.stack.findIndex((i) => i.element === snack);
            if (index >= 0) {
                this.stack.splice(index, 1);
            }
        }
    }
    static clear() {
        this.stack = [];
        if (this.current) {
            this.current.element.parentElement?.removeChild(this.current.element);
            this.current = undefined;
        }
    }
}
//# sourceMappingURL=SnackNotifications.js.map