// #region Imports
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Component } from '../Controls/Decorators/ComponentDecorator';
import { CustomElement } from '../Controls/Components/Abstracts/CustomElement';
import { theme2ElementTemplate } from './Theme2ElementTemplate';
import { theme2ElementStyle } from './Theme2ElementStyle';
import { Property } from '../Controls/Decorators/PropertyDecorator';
import { Watch } from '../Controls/Decorators/WatchDecorator';
import { Equals } from '../Equality/Equals';
import { ThemeService } from './ThemeService';
// #endregion
/**
 * The `{@link Theme2Element}` element.
 *
 * This element delegates all theme application logic to {@link ThemeService},
 * which can also be used independently without this element.
 *
 * @public
 */
let Theme2Element = class Theme2Element extends CustomElement {
    // #region Fields
    _name;
    _theme;
    _global;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._name = null;
        this._theme = null;
        this._global = false;
        ThemeService.instance.registerProvider(this);
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-theme2';
    }
    /**
     * Gets or sets the `name` property.
     * When set, this element will only respond to theme changes for this specific name.
     * When null (default), responds to global/default theme changes.
     *
     * @public
     */
    get name() {
        return this._name;
    }
    set name(value) {
        if (this._name !== value) {
            // Unregister with old ID
            ThemeService.instance.unregisterProvider(this);
            this._name = value;
            this.requestUpdate('name');
            // Re-register with new ID
            ThemeService.instance.registerProvider(this);
        }
    }
    /**
     * Gets or sets the `theme` property.
     * If no local theme is set, returns the theme from ThemeService for this element's name.
     *
     * @public
     */
    get theme() {
        if (this._theme) {
            return this._theme;
        }
        // Try to get theme from ThemeService
        try {
            return ThemeService.instance.getTheme(this._name);
        }
        catch {
            return null;
        }
    }
    set theme(value) {
        if (!Equals.areObjectsEqual(this._theme, value)) {
            this._theme = value;
            this.requestUpdate('theme');
        }
    }
    /**
     * Gets or sets the `global` property.
     * When true, CSS variables are applied both locally and globally.
     * When false (default), CSS variables are only applied locally to this element.
     *
     * @public
     */
    get global() {
        return this._global;
    }
    set global(value) {
        if (this._global !== value) {
            this._global = value;
            this.requestUpdate('global');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        ThemeService.instance.initialize();
        // If we have a theme, apply it through the service
        if (this._theme) {
            ThemeService.instance.applyTheme(this._theme, this._name, { notifyProviders: false });
        }
    }
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        ThemeService.instance.unregisterProvider(this);
    }
    /**
     * Applies the complete theme.
     * Called by ThemeService to update this provider's internal state.
     * CSS variables are handled by ThemeService directly.
     *
     * @public
     * @param theme The theme to apply.
     */
    applyTheme(theme) {
        if (!theme) {
            return;
        }
        this._theme = theme;
    }
    /**
     * Applies the scheme (light/dark mode color roles) from the theme.
     * Called by ThemeService to update this provider's internal state.
     *
     * @public
     * @param scheme The scheme to apply.
     */
    applyScheme(scheme) {
        if (this._theme) {
            this._theme.scheme = scheme;
        }
    }
    /**
     * Applies the color palette from the theme.
     * Called by ThemeService to update this provider's internal state.
     *
     * @public
     * @param palette The palette to apply.
     */
    applyPalette(palette) {
        if (this._theme) {
            this._theme.palette = palette;
        }
    }
    /**
     * Applies the font family from the theme.
     * Called by ThemeService to update this provider's internal state.
     *
     * @public
     * @param fontFamily The font family to apply.
     */
    applyFontFamily(fontFamily) {
        if (this._theme) {
            this._theme.fontFamily = fontFamily;
        }
    }
    /**
     * Applies the typography settings from the theme.
     * Called by ThemeService to update this provider's internal state.
     *
     * @public
     * @param typography The typography settings to apply.
     */
    applyTypography(typography) {
        if (this._theme) {
            this._theme.typography = typography;
        }
    }
    /**
     * Applies the layout settings from the theme.
     * Called by ThemeService to update this provider's internal state.
     *
     * @public
     * @param layout The layout settings to apply.
     */
    applyLayout(layout) {
        if (this._theme) {
            this._theme.layout = layout;
        }
    }
    /**
     * Applies the elevation (shadow) settings from the theme.
     * Called by ThemeService to update this provider's internal state.
     *
     * @public
     * @param elevation The elevation settings to apply.
     */
    applyElevation(elevation) {
        if (this._theme) {
            this._theme.elevation = elevation;
        }
    }
    /**
     * Called when the theme property changes.
     * Delegates to ThemeService for CSS variable management.
     *
     * @protected
     */
    onThemePropertyChanged(prev, next) {
        if (prev !== next && next) {
            // Delegate to ThemeService - it handles CSS variables for both global and local
            // Use notifyProviders: false to prevent infinite loop (we are the provider)
            ThemeService.instance.applyTheme(next, this._name, { notifyProviders: false });
        }
    }
};
__decorate([
    Property({
        type: String,
        attribute: 'name'
    }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], Theme2Element.prototype, "name", null);
__decorate([
    Property({ type: Object }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], Theme2Element.prototype, "theme", null);
__decorate([
    Property({ type: Boolean,
        useDefault: true }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], Theme2Element.prototype, "global", null);
__decorate([
    Watch('theme'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], Theme2Element.prototype, "onThemePropertyChanged", null);
Theme2Element = __decorate([
    Component({
        selector: 'mosaik-theme2',
        template: theme2ElementTemplate,
        styles: theme2ElementStyle,
        imports: []
    }),
    __metadata("design:paramtypes", [])
], Theme2Element);
export { Theme2Element };
//# sourceMappingURL=Theme2Element.js.map