import { balBrowser } from "../browser";
import { waitForComponent } from "../helpers";
export class BalNoticeController {
    constructor(options) {
        this.options = options;
        this.container = null;
        this.queue = [];
        this.preQueue = [];
        this.queueLimit = 5;
    }
    create(options) {
        if (balBrowser.hasDocument) {
            this.setupContainer(options);
            const clone = this.findClone(options);
            if (clone === undefined) {
                const el = document.createElement(this.options.tag);
                Object.assign(el, options);
                el.addEventListener('balClose', ev => {
                    this.removeFromQueue(ev.detail);
                });
                this.preQueue.push(el);
                this.updateQueue();
                return el;
            }
            return clone;
        }
    }
    setQueue(queueLimit) {
        this.queueLimit = queueLimit;
    }
    async dismissAll() {
        var _a;
        const elements = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelectorAll(this.options.tag);
        if (elements) {
            const closingQueue = [];
            for (let index = 0; index < elements.length; index++) {
                const el = elements[index];
                if (el.close) {
                    closingQueue.push(el.close());
                }
            }
            await Promise.all(closingQueue);
        }
    }
    /**
     * @deprecated use dismissAll instead
     */
    async clearAll() {
        console.warn('[DEPRECATED] - use dismissAll() instead');
        return this.dismissAll();
    }
    findClone(options) {
        for (let index = 0; index < this.queue.length; index++) {
            const el = this.queue[index];
            if (el.message === options.message && el.color === options.color) {
                return el;
            }
        }
        return undefined;
    }
    setupContainer(options) {
        if (balBrowser.hasDocument) {
            const containerId = `${this.options.tag}-container`;
            this.container = document.getElementById(containerId);
            if (this.container)
                return;
            if (!this.container) {
                this.container = document.createElement('bal-notices');
                this.container.setAttribute('interface', this.options.tag.replace('bal-', ''));
                this.container.setAttribute('animated', options.animated ? 'true' : 'false');
                if (options.container !== undefined) {
                    this.container.setAttribute('container', options.container === true ? '' : options.container);
                }
                this.container.id = containerId;
            }
            document.body.appendChild(this.container);
        }
    }
    async updateQueue() {
        var _a;
        if (this.queue.length < this.queueLimit) {
            const el = this.preQueue.shift();
            if (el && this.container) {
                await waitForComponent(this.container);
                const innerEl = this.container.querySelector('.bal-notices__inner');
                if (innerEl) {
                    this.queue.push(el);
                    innerEl.insertAdjacentElement('beforeend', el);
                }
            }
        }
        if (this.queue.length === 0) {
            (_a = this.container) === null || _a === void 0 ? void 0 : _a.remove();
        }
    }
    removeFromQueue(toastId) {
        this.queue = this.queue.filter(el => el.id !== toastId);
        setTimeout(() => this.updateQueue(), 0);
    }
}
