/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { PureEventEmitter } from '@breadstone/mosaik-elements';
// #endregion
/**
 * The `Theme2` class.
 *
 * @public
 */
export class Theme2 {
    // #region Fields
    static _themeElements = [];
    _themeChanged;
    _schemeChanged;
    _paletteChanged;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `Theme` class.
     *
     * @public
     */
    constructor() {
        this._themeChanged = new PureEventEmitter();
        this._schemeChanged = new PureEventEmitter();
        this._paletteChanged = new PureEventEmitter();
    }
    // #endregion
    // #region Properties
    /**
     * @public
     * @readonly
     * @eventProperty
     */
    get themeChanged() {
        return this._themeChanged;
    }
    /**
     * @public
     * @readonly
     * @eventProperty
     */
    get schemeChanged() {
        return this._schemeChanged;
    }
    /**
     * @public
     * @readonly
     * @eventProperty
     */
    get paletteChanged() {
        return this._paletteChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @static
     */
    static register(element) {
        Theme2._themeElements.push(element);
    }
    /**
     * @public
     * @static
     */
    static unregister(element) {
        const index = Theme2._themeElements.indexOf(element);
        if (index > -1) {
            Theme2._themeElements.splice(index, 1);
        }
    }
    /**
     * @public
     */
    getTheme() {
        const current = Theme2._themeElements.at(0)?.theme;
        if (!current) {
            throw new Error('No theme set.');
        }
        return current;
    }
    /**
     * Gets the current theme name from the document element.
     *
     * @public
     * @returns The current theme name or `null` if not set.
     */
    getDocumentTheme() {
        return document.documentElement.getAttribute('theme');
    }
    /**
     * Gets the current theme mode from the document element.
     *
     * @public
     * @returns The current theme mode or `null` if not set.
     */
    getDocumentThemeMode() {
        return document.documentElement.getAttribute('theme-mode');
    }
    /**
     * Applies the specified theme.
     *
     * @public
     * @param theme The theme to apply.
     */
    applyTheme(theme) {
        Theme2._themeElements.forEach((element) => {
            element.applyTheme(theme);
        });
        this._themeChanged.emit({ theme: theme });
    }
    /**
     * Applies the specified scheme.
     *
     * @public
     * @param scheme The scheme to apply.
     */
    applyScheme(scheme) {
        Theme2._themeElements.forEach((element) => {
            element.applyScheme(scheme);
        });
        this._schemeChanged.emit({ scheme: scheme });
    }
    /**
     * Applies the specified palette.
     *
     * @public
     * @param palette The palette to apply.
     */
    applyPalette(palette) {
        Theme2._themeElements.forEach((element) => {
            element.applyPalette(palette);
        });
        this._paletteChanged.emit({ palette: palette });
    }
    /**
     * Applies the specified font family.
     *
     * @public
     * @param fontFamily The font family to apply.
     */
    applyFontFamily(fontFamily) {
        Theme2._themeElements.forEach((element) => {
            element.applyFontFamily(fontFamily);
        });
    }
    /**
     * Applies the specified typography.
     *
     * @public
     * @param typography The typography to apply.
     */
    applyTypography(typography) {
        Theme2._themeElements.forEach((element) => {
            element.applyTypography(typography);
        });
    }
    /**
     * Applies the specified layout.
     *
     * @public
     * @param layout The layout to apply.
     */
    applyLayout(layout) {
        Theme2._themeElements.forEach((element) => {
            element.applyLayout(layout);
        });
    }
    /**
     * Applies the specified elevation.
     *
     * @public
     * @param elevation The elevation to apply.
     */
    applyElevation(elevation) {
        Theme2._themeElements.forEach((element) => {
            element.applyElevation(elevation);
        });
    }
    /**
     * Replaces the scheme with the specified key.
     * This is useful when you want to replace a specific scheme in the theme.
     *
     * @public
     * @param mode The theme mode to replace.
     * @param scheme The scheme color roles to replace.
     */
    replaceScheme(mode, scheme) {
        Theme2._themeElements.forEach((element) => {
            if (!element.theme?.scheme) {
                return;
            }
            const currentScheme = element.theme.scheme;
            const currentMode = currentScheme[mode];
            const mergedMode = Object.assign({}, currentMode, scheme);
            const updatedScheme = Object.assign({}, currentScheme, { [mode]: mergedMode });
            element.applyScheme(updatedScheme);
        });
        this._schemeChanged.emit({
            mode,
            scheme
        });
    }
    /**
     * Replaces the palette with the specified key.
     * This is useful when you want to replace a specific palette in the theme.
     *
     * @public
     * @param mode The theme mode to replace.
     * @param semantic The semantic color roles to replace.
     */
    replacePalette(mode, semantic) {
        Theme2._themeElements.forEach((element) => {
            if (!element.theme?.palette) {
                return;
            }
            const currentPalette = element.theme.palette;
            const currentMode = currentPalette[mode];
            const mergedMode = Object.assign({}, currentMode, semantic);
            const updatedPalette = Object.assign({}, currentPalette, { [mode]: mergedMode });
            element.applyPalette(updatedPalette);
        });
        this._paletteChanged.emit({
            mode,
            semantic
        });
    }
}
/**
 * @public
 */
export class Theme2ServiceLocator {
    // #region Fields
    static _current = null;
    // #endregion
    // #region Properties
    static get current() {
        if (!Theme2ServiceLocator.isSet()) {
            throw new Error('\'Theme\' not set.');
        }
        return Theme2ServiceLocator._current;
    }
    // #endregion
    // #region Methods
    static isSet() {
        return Theme2ServiceLocator._current !== null;
    }
    static set(current) {
        Theme2ServiceLocator._current = current;
    }
}
Theme2ServiceLocator.set(new Theme2());
//# sourceMappingURL=Theme2.js.map