// #region Imports
import { ControllerBase } from './Abstracts/ControllerBase';
// #endregion
/**
 * A controller that manages inline :host style variables using the CSSStyleSheet API.
 *
 * Initial styles should be declared in the component template.
 * This controller allows safe runtime get/set operations on CSS custom properties.
 */
export class InlineStyleController extends ControllerBase {
    // #region Fields
    _sheet;
    _rule;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `InlineStyleController` class.
     *
     * @public
     */
    constructor(host) {
        super(host);
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    hostConnected() {
        super.hostConnected();
        this._sheet = new CSSStyleSheet();
        this._sheet.replaceSync(':host {}');
        if (this.host.shadowRoot?.adoptedStyleSheets) {
            this.host.shadowRoot.adoptedStyleSheets = [
                ...this.host.shadowRoot.adoptedStyleSheets,
                this._sheet
            ];
        }
        this._rule = this._sheet.cssRules[0];
    }
    /**
     * Sets a CSS variable on the host element inside the Shadow DOM.
     *
     * @param name - The name of the variable (without "--").
     * @param value - The CSS value to assign.
     */
    set(name, value) {
        if (name.startsWith('--')) {
            throw new Error('CSS variable name should not start with "--".');
        }
        this._rule?.style.setProperty(`--${name}`, String(value));
    }
    /**
     * Gets a CSS variable's computed value from the host.
     *
     * @param name - The name of the variable (without "--").
     * @returns The computed value as a string, or `null` if not set.
     */
    get(name) {
        if (name.startsWith('--')) {
            throw new Error('CSS variable name should not start with "--".');
        }
        return this.host.shadowRoot
            ? window.getComputedStyle(this.host.shadowRoot.host).getPropertyValue(`--${name}`)
                .trim() || null
            : null;
    }
}
//# sourceMappingURL=InlineStyleController.js.map