import { BALOISE_ANIMATION_KEY } from "./config.const";
import { defaultConfig } from "./config.default";
export 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);
            }
        }
    }
}
export const config = /*@__PURE__*/ new Config();
export 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 {};
    }
};
