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;
};
import { customElement, html, property, state } from '../../../../../external/lit/index.js';
import { UmbChangeEvent } from '../../../event/index.js';
import { UmbLitElement } from '../../../lit-element/index.js';
import { umbExtensionsRegistry } from '../../../extension-registry/index.js';
import { UmbFormControlMixin } from '../../../validation/index.js';
import { UMB_DEFAULT_LOCALIZATION_CULTURE } from '../../../../../libs/localization-api/index.js';
let UmbUiCultureInputElement = class UmbUiCultureInputElement extends UmbFormControlMixin(UmbLitElement) {
    #invalidCulture;
    #invalidBaseCulture;
    set value(value) {
        if (value && typeof value === 'string') {
            const oldValue = super.value;
            super.value = value.toLowerCase();
            this.requestUpdate('value', oldValue);
        }
    }
    get value() {
        return super.value;
    }
    constructor() {
        super();
        this._options = [];
        this.observe(umbExtensionsRegistry.byType('localization'), (manifests) => {
            const options = manifests
                .filter((manifest) => !!manifest.meta.culture)
                .map((manifest) => {
                const culture = manifest.meta.culture.toLowerCase();
                return {
                    name: this.localize.term(`uiCulture_${culture}`),
                    value: culture,
                };
            });
            const distinct = [...new Map(options.map((item) => [item.value, item])).values()];
            this._options = distinct.sort((a, b) => a.name.localeCompare(b.name));
        }, 'umbObserveLocalizationManifests');
        this.addValidator('customError', () => this.localize.term('user_languageNotFound', this.#invalidCulture, this.value), () => !!this.#invalidCulture && !this.#invalidBaseCulture);
        this.addValidator('customError', () => this.localize.term('user_languageNotFoundFallback', this.#invalidCulture, this.#invalidBaseCulture), () => !!this.#invalidCulture && !!this.#invalidBaseCulture);
    }
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        if (this.value) {
            // Check if the culture can be found.
            const found = this._options.find((option) => option.value === this.value);
            if (!found) {
                this.#invalidCulture = this.value;
                // if not found, check for the base culture
                const locale = new Intl.Locale(this.value);
                if (locale.language) {
                    const foundBase = this._options.find((option) => option.value === locale.language);
                    if (foundBase) {
                        this.value = locale.language;
                    }
                    else {
                        // if the base culture is not found, set the value to "en"
                        this.#invalidBaseCulture = locale.language;
                        this.value = UMB_DEFAULT_LOCALIZATION_CULTURE;
                    }
                }
                else {
                    // if the base culture is not found, set the value to "en"
                    this.value = UMB_DEFAULT_LOCALIZATION_CULTURE;
                }
            }
        }
        this.addFormControlElement(this.shadowRoot.querySelector('uui-select'));
        this.checkValidity();
    }
    #onCustomValidationChange(event) {
        this.#invalidCulture = undefined;
        this.#invalidBaseCulture = undefined;
        this.value = event.target.value.toString();
        this.dispatchEvent(new UmbChangeEvent());
    }
    render() {
        return html `
			<uui-select
				.options=${this._options.map((e) => ({ ...e, selected: e.value == this.value }))}
				@change=${this.#onCustomValidationChange}>
			</uui-select>
		`;
    }
};
__decorate([
    state()
], UmbUiCultureInputElement.prototype, "_options", void 0);
__decorate([
    property({ type: String })
], UmbUiCultureInputElement.prototype, "value", null);
UmbUiCultureInputElement = __decorate([
    customElement('umb-ui-culture-input')
], UmbUiCultureInputElement);
export { UmbUiCultureInputElement };
export default UmbUiCultureInputElement;
