import { a as setPlatformHelpers } from './index-e04a7bc5.js';
import { b as balBrowser } from './browser-a623d3a0.js';
import { d as defaultConfig } from './config.default-7d48bb87.js';

class BalNoticeController {
    constructor(options) {
        this.options = options;
        this.container = null;
        this.queue = [];
        this.preQueue = [];
        this.queueLimit = 5;
    }
    create(options) {
        if (balBrowser.hasDocument) {
            this.setupContainer();
            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);
        }
    }
    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() {
        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.id = containerId;
            }
            document.body.appendChild(this.container);
        }
    }
    updateQueue() {
        var _a;
        if (this.queue.length < this.queueLimit) {
            const el = this.preQueue.shift();
            if (el && this.container) {
                this.queue.push(el);
                this.container.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);
    }
}

class BalToastController extends BalNoticeController {
    constructor() {
        super({
            tag: 'bal-toast',
        });
    }
    create(options) {
        return super.create(options);
    }
}
const balToastController = new BalToastController();

class BalSnackbarController extends BalNoticeController {
    constructor() {
        super({
            tag: 'bal-snackbar',
        });
    }
    create(options) {
        return super.create(options);
    }
}
const balSnackbarController = new BalSnackbarController();

const BALOISE_SESSION_KEY = 'baloise-persist-config';

class Config {
    constructor() {
        this._componentObservers = [];
        this._observers = [];
        this._config = defaultConfig;
    }
    get locale() {
        return `${this._config.language}-${this._config.region}`;
    }
    get region() {
        return this._config.region;
    }
    set region(region) {
        if (region !== this._config.region) {
            this._config.region = region;
            this._notify();
        }
    }
    get language() {
        return this._config.language;
    }
    set language(language) {
        if (language !== this._config.language) {
            if (this._config.allowedLanguages.includes(language)) {
                this._config.language = language;
            }
            else {
                this._config.language = this._config.fallbackLanguage;
            }
            this._notify();
        }
    }
    get allowedLanguages() {
        return this._config.allowedLanguages;
    }
    set allowedLanguages(allowedLanguages) {
        if (allowedLanguages !== this._config.allowedLanguages) {
            this._config.allowedLanguages = allowedLanguages;
            this._notify();
        }
    }
    get icons() {
        return this._config.icons;
    }
    set icons(icons) {
        this._config.icons = Object.assign(Object.assign({}, this._config.icons), icons);
        this._notify();
    }
    get logger() {
        return this._config.logger;
    }
    set logger(logger) {
        this._config.logger = Object.assign({}, logger);
        this._notify();
    }
    get animated() {
        return this._config.animated;
    }
    set animated(animated) {
        this._config.animated = animated;
        this._notify();
    }
    attach(observer) {
        const isExist = this._observers.includes(observer);
        if (isExist) {
            return console.log('Subject: Observer has been attached already.');
        }
        this._observers.push(observer);
        observer.configChanged(this._config);
    }
    detach(observer) {
        const observerIndex = this._observers.indexOf(observer);
        if (observerIndex === -1) {
            return console.log('Subject: Nonexistent observer.');
        }
        this._observers.splice(observerIndex, 1);
    }
    attachComponent(observer) {
        const isExist = this._componentObservers.includes(observer);
        if (isExist) {
            return console.log('Subject: Observer has been attached already.');
        }
        this._componentObservers.push(observer);
        observer.configChanged(this._config);
    }
    detachComponent(observer) {
        const observerIndex = this._componentObservers.indexOf(observer);
        if (observerIndex === -1) {
            return console.log('Subject: Nonexistent observer.');
        }
        this._componentObservers.splice(observerIndex, 1);
    }
    toString() {
        return JSON.stringify(this._config);
    }
    reset(config) {
        this._config = Object.assign(Object.assign(Object.assign({}, this._config), config), { icons: Object.assign(Object.assign({}, this._config.icons), config.icons) });
        this._notify(false);
    }
    _notify(all = true) {
        for (const observer of this._componentObservers) {
            observer.configChanged(this._config);
        }
        if (all) {
            for (const observer of this._observers) {
                observer.configChanged(this._config);
            }
        }
        if (balBrowser.hasWindow) {
            saveConfig(window, this._config);
        }
    }
}
const config = new Config();
const configFromSession = (win) => {
    try {
        const configStr = win.sessionStorage.getItem(BALOISE_SESSION_KEY);
        return configStr !== null ? JSON.parse(configStr) : {};
    }
    catch (e) {
        return {};
    }
};
const saveConfig = (win, c) => {
    try {
        win.sessionStorage.setItem(BALOISE_SESSION_KEY, JSON.stringify(c));
    }
    catch (e) {
        return;
    }
};

const setupConfig = (userConfig = {}, win = {}) => {
    if (Object.keys(win).length === 0 && balBrowser.hasWindow) {
        win = window;
    }
    win.BaloiseDesignSystem = win.BaloiseDesignSystem || {};
    config.reset(Object.assign(Object.assign(Object.assign({}, configFromSession(win)), userConfig), { icons: Object.assign(Object.assign({}, configFromSession(win).icons), userConfig.icons) }));
    win.BaloiseDesignSystem.config = config;
};

const VERSION = '14.5.1';

const initializeBaloiseDesignSystem = (userConfig = {}, platformConfig = undefined, win = {}) => {
    if (Object.keys(win).length === 0 && balBrowser.hasWindow) {
        win = window;
    }
    win.BaloiseDesignSystem = win.BaloiseDesignSystem || {};
    if (platformConfig) {
        setPlatformHelpers(platformConfig);
    }
    setupConfig(Object.assign(Object.assign({}, userConfig), platformConfig), win);
    win.BaloiseDesignSystem.toastController = balToastController;
    win.BaloiseDesignSystem.snackbarController = balSnackbarController;
    win.BaloiseDesignSystem.initialize = () => setupConfig(win.BaloiseDesignSystem.config, win);
    win.BaloiseDesignSystem.version = VERSION;
};

export { BalToastController as B, BalSnackbarController as a, balToastController as b, balSnackbarController as c, initializeBaloiseDesignSystem as i, setupConfig as s };
