import { a as setPlatformHelpers } from './index-BdVwMG9u.js';
import { b as balBrowser } from './browser-B1u-Mq75.js';
import { f as waitForComponent } from './helpers-BNj87EbG.js';
import { d as defaultConfig } from './config.default-RE8-gLMQ.js';

const BALOISE_ANIMATION_KEY = 'baloise-animated';

class Config {
    constructor() {
        this._componentObservers = [];
        this._observers = [];
        this._config = defaultConfig;
    }
    get brand() {
        return this._config.brand;
    }
    set brand(brand) {
        if (brand !== this._config.brand) {
            this._config.brand = brand;
            this._notify();
        }
    }
    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();
    }
    get httpFormSubmit() {
        return this._config.httpFormSubmit;
    }
    set httpFormSubmit(httpFormSubmit) {
        this._config.httpFormSubmit = httpFormSubmit;
        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);
            }
        }
    }
}
const config = /*@__PURE__*/ new Config();
const configFromLocalStorage = (win) => {
    try {
        const value = win.localStorage.getItem(BALOISE_ANIMATION_KEY) || 'true';
        const animated = JSON.parse(value);
        if (animated !== null) {
            return {
                animated,
            };
        }
        return {};
    }
    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({}, configFromLocalStorage(win)), userConfig), { icons: Object.assign({}, userConfig.icons) }));
    win.BaloiseDesignSystem.config = config;
};

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);
    }
}

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

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

const tags = [
    "bal-accordion",
    "bal-accordion-details",
    "bal-accordion-summary",
    "bal-accordion-trigger",
    "bal-app",
    "bal-badge",
    "bal-button",
    "bal-button-group",
    "bal-card",
    "bal-card-actions",
    "bal-card-button",
    "bal-card-content",
    "bal-card-subtitle",
    "bal-card-title",
    "bal-carousel",
    "bal-carousel-item",
    "bal-check",
    "bal-checkbox",
    "bal-checkbox-group",
    "bal-close",
    "bal-content",
    "bal-data",
    "bal-data-item",
    "bal-data-label",
    "bal-data-value",
    "bal-date",
    "bal-date-calendar",
    "bal-date-calendar-cell",
    "bal-divider",
    "bal-dropdown",
    "bal-field",
    "bal-field-control",
    "bal-field-hint",
    "bal-field-label",
    "bal-field-message",
    "bal-file-upload",
    "bal-footer",
    "bal-form",
    "bal-form-col",
    "bal-form-grid",
    "bal-heading",
    "bal-hint",
    "bal-hint-text",
    "bal-hint-title",
    "bal-icon",
    "bal-input",
    "bal-input-date",
    "bal-input-group",
    "bal-input-slider",
    "bal-input-stepper",
    "bal-label",
    "bal-list",
    "bal-list-item",
    "bal-list-item-accordion-body",
    "bal-list-item-accordion-head",
    "bal-list-item-content",
    "bal-list-item-icon",
    "bal-list-item-subtitle",
    "bal-list-item-title",
    "bal-logo",
    "bal-modal",
    "bal-modal-body",
    "bal-modal-header",
    "bal-nav",
    "bal-nav-link",
    "bal-nav-link-grid",
    "bal-nav-link-grid-col",
    "bal-nav-link-group",
    "bal-nav-menu-bar",
    "bal-nav-menu-flyout",
    "bal-nav-meta-bar",
    "bal-navbar",
    "bal-navbar-brand",
    "bal-navbar-menu",
    "bal-navbar-menu-end",
    "bal-navbar-menu-start",
    "bal-notices",
    "bal-notification",
    "bal-number-input",
    "bal-option",
    "bal-option-list",
    "bal-pagination",
    "bal-popover",
    "bal-popover-content",
    "bal-popup",
    "bal-progress-bar",
    "bal-radio",
    "bal-radio-group",
    "bal-radio-icon",
    "bal-segment",
    "bal-segment-item",
    "bal-select",
    "bal-select-option",
    "bal-shape",
    "bal-sheet",
    "bal-snackbar",
    "bal-spinner",
    "bal-stack",
    "bal-stage",
    "bal-stage-back-link",
    "bal-stage-body",
    "bal-stage-foot",
    "bal-stage-head",
    "bal-stage-image",
    "bal-step-item",
    "bal-steps",
    "bal-switch",
    "bal-tab-item",
    "bal-table",
    "bal-tabs",
    "bal-tag",
    "bal-tag-group",
    "bal-text",
    "bal-textarea",
    "bal-time-input",
    "bal-toast",
    "bal-tooltip"
];

// generated by .build/version.js
const VERSION = '19.10.0';

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;
    if (userConfig._generateHydrateForCustomElementsOutput) {
        generateHydrateForCustomElementsOutput(tags);
    }
};
/**
 * This function generates the CSS needed for custom elements hydration and inserts it into the document head.
 * The CSS rules include styles for slot fallback and the hydrated class.
 *
 * This is copied from the Stencil core library.
 * src/runtime/bootstrap-lazy.ts
 */
const generateHydrateForCustomElementsOutput = (cmpTags = []) => {
    const SLOT_FB_CSS = 'slot-fb{display:contents}slot-fb[hidden]{display:none}';
    const HYDRATED_CSS = '{visibility:hidden}.hydrated{visibility:inherit}';
    const win = typeof window !== 'undefined' ? window : {};
    if (!win.document) {
        console.warn('Stencil: No document found. Skipping bootstrapping lazy components.');
        return;
    }
    const head = win.document.head;
    const metaCharset = /* @__PURE__ */ head.querySelector('meta[charset]');
    const dataStyles = /* @__PURE__ */ win.document.createElement('style');
    if (cmpTags.length > 0) {
        dataStyles.textContent += SLOT_FB_CSS;
        dataStyles.textContent += cmpTags.sort() + HYDRATED_CSS;
        if (dataStyles.innerHTML.length) {
            dataStyles.setAttribute('data-styles', '');
            head.insertBefore(dataStyles, metaCharset ? metaCharset.nextSibling : head.firstChild);
        }
    }
};

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