// #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 { CustomElement } from '../Controls/Components/Abstracts/CustomElement';
import { Component } from '../Controls/Decorators/ComponentDecorator';
import { Property } from '../Controls/Decorators/PropertyDecorator';
import { Watch } from '../Controls/Decorators/WatchDecorator';
import { Equals } from '../Equality/Equals';
import { theme2ElementStyle } from './Theme2ElementStyle';
import { theme2ElementTemplate } from './Theme2ElementTemplate';
import { ThemeService } from './ThemeService';
// #endregion
/**
 * Theme2 - A scoped theme boundary for Mosaik components.
 *
 * @description
 * `mosaik-theme2` connects a complete `ITheme` object with the `ThemeService`.
 * The element itself renders only its default slot and does not create visible UI.
 * When a theme is assigned, `ThemeService` expands the scheme, palette,
 * typography, layout, font, and elevation values into CSS custom properties.
 * It writes both theme-prefixed variables such as `--joy-scheme-background`
 * and neutral Mosaik variables such as `--mosaik-scheme-background`.
 *
 * @remarks
 * By default, the generated CSS variables are written to the `mosaik-theme2`
 * host only. They inherit through the slotted subtree, so this creates a local
 * theme island without changing the rest of the application. When `global` is
 * set, the same variables are also written to `document.documentElement`, making
 * the theme available to the whole application.
 *
 * The active color mode comes from the document `theme-mode` attribute. If the
 * document is in light mode and a nested area should look dark, assign an inline
 * theme whose active `light` branch contains the dark scheme, palette, and
 * elevation values. Do not set `global` on that nested wrapper, otherwise the
 * dark variables would be promoted to the whole document.
 *
 * Providers with the same `name` share the same stored theme state in
 * `ThemeService`. A provider without a `name` belongs to the default scope.
 *
 * @name Theme2
 * @element mosaik-theme2
 * @category Theming
 *
 * @slot - The content that receives the scoped theme variables.
 *
 * @example
 * Application shell that applies a complete theme globally:
 * ```html
 * <html lang="en" dir="ltr" theme="joy" theme-mode="light">
 *   <body>
 *     <mosaik-theme2 id="application-theme" global>
 *       <mosaik-app>
 *         <mosaik-text text="The complete application uses the Joy theme."></mosaik-text>
 *       </mosaik-app>
 *     </mosaik-theme2>
 *
 *     <script type="module">
 *       import { JoyTheme } from '@breadstone/mosaik-themes';
 *
 *       const applicationTheme = document.querySelector('#application-theme');
 *       applicationTheme.theme = JoyTheme.JOY_THEME;
 *     </script>
 *   </body>
 * </html>
 * ```
 *
 * @example
 * Standalone usage through ThemeService when no wrapper element is needed:
 * ```html
 * <html lang="en" dir="ltr" theme="joy" theme-mode="light">
 *   <body>
 *     <mosaik-app>
 *       <mosaik-text text="The theme is applied directly by ThemeService."></mosaik-text>
 *     </mosaik-app>
 *
 *     <script type="module">
 *       import { ThemeService } from '@breadstone/mosaik-elements-foundation';
 *       import { JoyTheme } from '@breadstone/mosaik-themes';
 *
 *       ThemeService.configure({
 *         defaultTheme: JoyTheme.JOY_THEME,
 *         defaultThemeMode: 'light',
 *         autoInitialize: false
 *       });
 *
 *       ThemeService.instance.initialize();
 *     </script>
 *   </body>
 * </html>
 * ```
 *
 * @example
 * Local theme island that does not modify the application theme:
 * ```html
 * <mosaik-theme2 id="marketing-theme" name="marketing">
 *   <section
 *     style="background: var(--mosaik-scheme-background); color: var(--mosaik-scheme-foreground); padding: var(--mosaik-layout-space);"
 *   >
 *     <mosaik-text text="Only this section receives the scoped marketing theme."></mosaik-text>
 *     <mosaik-button label="Continue" variant="primary"></mosaik-button>
 *   </section>
 * </mosaik-theme2>
 *
 * <script type="module">
 *   import { JoyTheme } from '@breadstone/mosaik-themes';
 *
 *   const marketingTheme = document.querySelector('#marketing-theme');
 *   marketingTheme.theme = {
 *     ...JoyTheme.JOY_THEME,
 *     name: 'marketing'
 *   };
 * </script>
 * ```
 *
 * @example
 * Dark inline section inside a light application:
 * ```html
 * <html lang="en" dir="ltr" theme="joy" theme-mode="light">
 *   <body>
 *     <mosaik-theme2 id="application-theme" global>
 *       <mosaik-text text="This part follows the light application theme."></mosaik-text>
 *
 *       <mosaik-theme2 id="dark-inline-theme" name="dark-inline">
 *         <section
 *           style="background: var(--mosaik-scheme-background); color: var(--mosaik-scheme-foreground); padding: var(--mosaik-layout-space);"
 *         >
 *           <mosaik-text text="This nested section renders with dark values while the document remains light."></mosaik-text>
 *           <mosaik-button label="Review" variant="primary"></mosaik-button>
 *         </section>
 *       </mosaik-theme2>
 *     </mosaik-theme2>
 *
 *     <script type="module">
 *       import { JoyTheme } from '@breadstone/mosaik-themes';
 *
 *       const applicationTheme = document.querySelector('#application-theme');
 *       const darkInlineTheme = document.querySelector('#dark-inline-theme');
 *
 *       applicationTheme.theme = JoyTheme.JOY_THEME;
 *       darkInlineTheme.theme = {
 *         ...JoyTheme.JOY_THEME,
 *         name: 'joy-inline-dark',
 *         scheme: {
 *           ...JoyTheme.JOY_THEME.scheme,
 *           light: JoyTheme.JOY_THEME.scheme.dark
 *         },
 *         palette: {
 *           ...JoyTheme.JOY_THEME.palette,
 *           light: JoyTheme.JOY_THEME.palette.dark
 *         },
 *         elevation: {
 *           ...JoyTheme.JOY_THEME.elevation,
 *           light: JoyTheme.JOY_THEME.elevation.dark
 *         }
 *       };
 *     </script>
 *   </body>
 * </html>
 * ```
 *
 * @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 `is` property.
     * The `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 provider participates in the matching `ThemeService` scope.
     * Providers with the same name share stored theme state and receive updates
     * for that name. When null, the provider participates in the default scope.
     *
     * @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.
     * This property expects a complete `ITheme` object and cannot be expressed
     * as a plain HTML attribute. Assign it from JavaScript, for example:
     * `document.querySelector('mosaik-theme2').theme = JoyTheme.JOY_THEME;`.
     * If no local theme is set, the getter returns the theme stored in
     * `ThemeService` for this element's `name` scope.
     *
     * @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 to this provider and
     * globally to `document.documentElement`. When false, CSS variables are only
     * applied locally to this provider and inherit through its slotted content.
     * The property can be set with the boolean HTML attribute `global`.
     *
     * @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, applyGlobally: this._global });
        }
    }
    /**
     * @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)
            // Use applyGlobally to respect the provider's global flag
            ThemeService.instance.applyTheme(next, this._name, { notifyProviders: false, applyGlobally: this._global });
        }
    }
};
__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