// #region Imports
import { ContextConsumer } from '@lit/context';
import { ControllerBase } from './Abstracts/ControllerBase';
/**
 * Controller for child components that consume inherited properties from parent providers.
 *
 * This controller:
 * - Consumes properties from a `@lit/context` provider
 * - Tracks which properties have been explicitly set on the host
 * - Returns the effective value: explicit > inherited > local default
 *
 * @example
 * ```typescript
 * class AvatarElement extends CustomElement {
 *     private _inheritance = addInheritanceConsumerController(this, {
 *         context: AvatarGroupInheritanceContext
 *     });
 *
 *     public override get size(): Size {
 *         return this._inheritance.get('size', super.size);
 *     }
 *
 *     public override set size(value: Size) {
 *         this._inheritance.markExplicit('size');
 *         super.size = value;
 *     }
 * }
 * ```
 *
 * @public
 */
export class InheritanceConsumerController extends ControllerBase {
    // #region Fields
    _explicitlySet;
    _consumer;
    _inherited;
    // #endregion
    // #region Ctor
    /**
     * Creates a new InheritanceConsumerController.
     *
     * @public
     * @param host The host element.
     * @param config The controller configuration.
     */
    constructor(host, config) {
        super(host);
        this._explicitlySet = new Set();
        this._inherited = {};
        this._consumer = new ContextConsumer(this.host, {
            context: config.context,
            subscribe: true,
            callback: (value) => {
                const prev = this._inherited;
                this._inherited = value;
                // Trigger requestUpdate for each changed property to enable @Watch callbacks
                for (const key of Object.keys(value)) {
                    if (!this._explicitlySet.has(key) && prev[key] !== value[key]) {
                        this.host.requestUpdate(key, prev[key]);
                    }
                }
            }
        });
    }
    // #endregion
    // #region Properties
    /**
     * Whether this consumer has found a provider.
     *
     * @public
     * @readonly
     */
    get hasProvider() {
        return Object.keys(this._inherited).length > 0;
    }
    /**
     * Gets all inherited properties.
     *
     * @public
     * @readonly
     */
    get inherited() {
        return this._inherited;
    }
    /**
     * Gets the context consumer.
     *
     * @protected
     * @readonly
     */
    get consumer() {
        return this._consumer;
    }
    // #endregion
    // #region Methods
    /**
     * Marks a property as explicitly set on the host.
     * Once marked, the inherited value will be ignored for this property.
     *
     * @public
     * @param key The property key.
     */
    markExplicit(key) {
        this._explicitlySet.add(key);
    }
    /**
     * Checks if a property has been explicitly set.
     *
     * @public
     * @param key The property key.
     * @returns True if the property was explicitly set.
     */
    isExplicit(key) {
        return this._explicitlySet.has(key);
    }
    /**
     * Gets the effective value for a property.
     *
     * Priority: explicit > inherited > localValue
     *
     * @public
     * @param key The property key.
     * @param localValue The local value from the host.
     * @returns The effective value (same type as localValue).
     */
    get(key, localValue) {
        // If explicitly set on host, use local value
        if (this._explicitlySet.has(key)) {
            return localValue;
        }
        // If inherited from provider, use inherited value
        const inheritedValue = this._inherited[key];
        if (inheritedValue !== undefined) {
            return inheritedValue;
        }
        // Fallback to local value
        return localValue;
    }
    /**
     * Resets the explicit flag for a property.
     * After reset, the property will again inherit from the provider.
     *
     * @public
     * @param key The property key.
     */
    resetExplicit(key) {
        this._explicitlySet.delete(key);
        this.host.requestUpdate();
    }
    /**
     * Resets all explicit flags.
     *
     * @public
     */
    resetAllExplicit() {
        this._explicitlySet.clear();
        this.host.requestUpdate();
    }
}
/**
 * Adds an inheritance consumer controller to the given host.
 *
 * @public
 * @param host The host element.
 * @param config The configuration options.
 * @returns The inheritance consumer controller instance.
 */
export function addInheritanceConsumerController(host, config) {
    return new InheritanceConsumerController(host, config);
}
//# sourceMappingURL=InheritanceConsumerController.js.map