import { UMB_PROPERTY_DATASET_CONTEXT } from './property-dataset-context.token.js';
import { UmbContextBase } from '../../../../libs/class-api/index.js';
import { UmbArrayState, UmbBooleanState, UmbStringState } from '../../../../libs/observable-api/index.js';
import { UmbVariantContext, UmbVariantId } from '../../variant/index.js';
/**
 * A base property dataset context implementation.
 * @class UmbPropertyDatasetContextBase
 * @augments {UmbContextBase}
 */
export class UmbPropertyDatasetContextBase extends UmbContextBase {
    #name;
    #properties;
    #readOnly;
    #variantId;
    #variantContext;
    getEntityType() {
        return this._entityType;
    }
    getUnique() {
        return this._unique;
    }
    getName() {
        return this.#name.getValue();
    }
    setName(name) {
        this.#name.setValue(name);
    }
    getVariantId() {
        return this.#variantId;
    }
    // variant id for a specific property?
    constructor(host) {
        // The controller alias, is a very generic name cause we want only one of these for this controller host.
        super(host, UMB_PROPERTY_DATASET_CONTEXT);
        this.#name = new UmbStringState(undefined);
        this.name = this.#name.asObservable();
        this.#properties = new UmbArrayState([], (x) => x.alias);
        this.properties = this.#properties.asObservable();
        /**
         * @deprecated - use `properties` instead.
         */
        this.values = this.properties;
        this.#readOnly = new UmbBooleanState(false);
        this.readOnly = this.#readOnly.asObservable();
        this.#variantId = UmbVariantId.CreateInvariant();
        this.#variantContext = new UmbVariantContext(this).inherit();
        this.#variantContext.setVariantId(this.getVariantId());
    }
    /**
     * @function propertyValueByAlias
     * @param {string} propertyAlias - the alias to observe
     * @returns {Promise<Observable<ReturnType | undefined> | undefined>} - an Observable for the value of this property.
     */
    async propertyValueByAlias(propertyAlias) {
        return this.#properties.asObservablePart((values) => {
            const valueObj = values.find((x) => x.alias === propertyAlias);
            return valueObj ? valueObj.value : undefined;
        });
    }
    /**
     * @function setPropertyValue
     * @param {string} alias - The alias to set this value for
     * @param {PromiseLike<unknown>} value - value can be a promise resolving into the actual value or the raw value it self.
     * @description Set the value of this property.
     */
    setPropertyValue(alias, value) {
        this.#properties.appendOne({ alias, value });
    }
    /**
     * @deprecated Use `getProperties`
     * @returns {Array<UmbPropertyValueData>} - Array of properties as objects with alias and value properties.
     */
    getValues() {
        return this.#properties.getValue();
    }
    /**
     * @param {Array<UmbPropertyValueData>} properties - Properties array with alias and value properties.
     * @deprecated Use `setProperties`
     */
    setValues(properties) {
        this.#properties.setValue(properties);
    }
    /**
     * @returns {Array<UmbPropertyValueData>} - Array of properties as objects with alias and value properties.
     */
    async getProperties() {
        return this.#properties.getValue();
    }
    /**
     * @param {Array<UmbPropertyValueData>} properties - Properties array with alias and value properties.
     */
    setProperties(properties) {
        this.#properties.setValue(properties);
    }
    /**
     * Gets the read-only state of the current variant culture.
     * @returns {*}  {boolean}
     */
    getReadOnly() {
        return this.#readOnly.getValue();
    }
}
