import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { UmbArrayState } from '../../../../libs/observable-api/index.js';
import { UMB_VARIANT_CONTEXT } from '../../../core/variant/index.js';
/**
 * A controller for resolving data for document urls
 * @exports
 * @class UmbDocumentUrlsDataResolver
 * @augments {UmbControllerBase}
 */
export class UmbDocumentUrlsDataResolver extends UmbControllerBase {
    #appCulture;
    #variantId;
    #displayVariantId;
    #data;
    #init;
    #urls;
    constructor(host) {
        super(host);
        this.#urls = new UmbArrayState([], (url) => url.url);
        /**
         * The urls for the current culture
         * @returns {ObservableArray<UmbDocumentUrlModel>} The urls for the current culture
         * @memberof UmbDocumentUrlsDataResolver
         */
        this.urls = this.#urls.asObservable();
        this.#init = Promise.all([
            this.consumeContext(UMB_VARIANT_CONTEXT, async (context) => {
                this.#variantId = await context?.getVariantId();
                this.#displayVariantId = await this.observe(context?.displayVariantId)?.asPromise();
                this.#setCultureAwareValues();
            }).asPromise(),
        ]);
    }
    /**
     * Get the current data
     * @returns {Array<UmbDocumentUrlModel> | undefined} The current data
     * @memberof UmbDocumentUrlsDataResolver
     */
    getData() {
        return this.#data;
    }
    /**
     * Set the current data
     * @param {Array<UmbDocumentUrlModel> | undefined} data The current data
     * @memberof UmbDocumentUrlsDataResolver
     */
    setData(data) {
        this.#data = data;
        if (!this.#data) {
            this.#urls.setValue([]);
            return;
        }
        this.#setCultureAwareValues();
    }
    /**
     * Get the urls for the current culture
     * @returns {(Promise<Array<UmbDocumentUrlModel> | []>)} The urls for the current culture
     * @memberof UmbDocumentUrlsDataResolver
     */
    async getUrls() {
        await this.#init;
        return this.#urls.getValue();
    }
    #setCultureAwareValues() {
        this.#setUrls();
    }
    #setUrls() {
        const data = this.#getDataForCurrentCulture();
        this.#urls.setValue(data ?? []);
    }
    #getCurrentCulture() {
        return this.#variantId?.culture || this.#displayVariantId?.culture || this.#appCulture;
    }
    #getDataForCurrentCulture() {
        const culture = this.#getCurrentCulture();
        // If there is no culture context (invariant data) we return all urls
        return culture ? this.#data?.filter((x) => x.culture === culture) : this.#data;
    }
}
