/* eslint-disable no-restricted-syntax */
// #endregion
/**
 * Provides a clean and extensible interface to manage global CSS custom properties
 * via a dedicated <style> tag inside <head>.
 *
 * @public
 */
export class GlobalStyleSheet {
    // #region Fields
    _styleId;
    _styleElement = null;
    _rootRule = null;
    _initialized;
    // #endregion
    // #region Ctor
    /**
     * Constructs an uninitialized instance of the GlobalStyleSheet.
     * Call `.initialize()` before using it.
     */
    constructor(styleId = 'global-style-vars') {
        this._styleId = styleId;
        this._styleElement = null;
        this._rootRule = null;
        this._initialized = false;
    }
    // #endregion
    // #region Methods
    /**
     * Initializes the stylesheet and inserts a :root rule if not present.
     *
     * @public
     */
    initialize() {
        if (this._initialized) {
            return;
        }
        const existing = document.getElementById(this._styleId);
        this._styleElement = existing ?? this.createStyleElement(this._styleId);
        const sheet = this._styleElement.sheet;
        if (!sheet) {
            throw new Error('Stylesheet could not be accessed from style element.');
        }
        if (sheet.cssRules.length === 0) {
            sheet.insertRule(':root {}', 0);
        }
        const rule = sheet.cssRules[0];
        if (!(rule instanceof CSSStyleRule) || rule.selectorText !== ':root') {
            throw new Error('Failed to initialize :root rule for GlobalStyleSheet.');
        }
        this._rootRule = rule;
        this._initialized = true;
    }
    /**
     * Disposes the style element and clears all references.
     *
     * @public
     */
    dispose() {
        if (this._styleElement?.isConnected) {
            this._styleElement.remove();
        }
        this._styleElement = null;
        this._rootRule = null;
        this._initialized = false;
    }
    /**
     * Sets a CSS variable on the :root rule.
     *
     * @public
     * @param name - The name of the CSS variable (must start with `--`).
     * @param value - The value of the CSS variable.
     */
    setVariable(name, value) {
        this.ensureInitialized();
        this.ensureValidVariableName(name);
        const current = this._rootRule?.style.getPropertyValue(name);
        if (current?.trim() !== value.trim()) {
            this._rootRule?.style.setProperty(name, value);
        }
    }
    /**
     * Retrieves the value of a CSS variable from the :root rule.
     *
     * @public
     * @param name - The name of the CSS variable (must start with `--`).
     * @returns The value of the CSS variable or null if not set.
     */
    getVariable(name) {
        this.ensureInitialized();
        this.ensureValidVariableName(name);
        return this._rootRule?.style.getPropertyValue(name) ?? null;
    }
    /**
     * Checks if a CSS variable is set on the :root rule.
     *
     * @public
     * @param name - The name of the CSS variable (must start with `--`).
     * @returns True if the variable is set, false otherwise.
     */
    hasVariable(name) {
        this.ensureInitialized();
        this.ensureValidVariableName(name);
        return this._rootRule?.style.getPropertyValue(name).trim() !== '';
    }
    /**
     * Removes a CSS variable from the :root rule.
     *
     * @public
     * @param name - The name of the CSS variable (must start with `--`).
     */
    removeVariable(name) {
        this.ensureInitialized();
        this.ensureValidVariableName(name);
        this._rootRule?.style.removeProperty(name);
    }
    /**
     * Clears all CSS variables from the :root rule.
     *
     * @private
     * @param name - The name of the CSS variable (must start with `--`).
     */
    createStyleElement(id) {
        const style = document.createElement('style');
        style.id = id;
        document.head.appendChild(style);
        return style;
    }
    /**
     * Ensures the variable name is valid (starts with `--`).
     *
     * @private
     * @param name - The name of the CSS variable.
     */
    ensureValidVariableName(name) {
        if (!name.startsWith('--')) {
            throw new Error(`Invalid CSS variable name: "${name}". Variables must start with "--".`);
        }
    }
    /**
     * Ensures the GlobalStyleSheet is initialized.
     *
     * @private
     */
    ensureInitialized() {
        if (!this._initialized || !this._rootRule) {
            throw new Error('GlobalStyleSheet must be initialized before use. Call initialize() first.');
        }
    }
}
//# sourceMappingURL=StyleSheet.js.map