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 { updateItemsSelectedState } from '../utils/property-editor-ui-state-manager.js';
import { customElement, html, property, state } from '../../../external/lit/index.js';
import { UmbLitElement } from '../../core/lit-element/index.js';
import { UmbChangeEvent } from '../../core/event/index.js';
/**
 * @element umb-property-editor-ui-select
 */
let UmbPropertyEditorUISelectElement = class UmbPropertyEditorUISelectElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        this._value = '';
        this._options = [];
    }
    // Update the selected state of existing options when value changes
    set value(newValue) {
        this._value = newValue || '';
        this.#updateSelectedState();
    }
    get value() {
        return this._value;
    }
    set config(config) {
        if (!config)
            return;
        const items = config.getValueByAlias('items');
        if (Array.isArray(items) && items.length > 0) {
            this._options =
                typeof items[0] === 'string'
                    ? items.map((item) => ({ name: item, value: item, selected: item === this._value }))
                    : items.map((item) => ({ name: item.name, value: item.value, selected: item.value === this._value }));
        }
    }
    #onChange(event) {
        this.value = event.target.value;
        this.dispatchEvent(new UmbChangeEvent());
    }
    /**
     * Updates the selected state of all options based on current value.
     * This fixes the issue where UI doesn't update when values are set programmatically.
     */
    #updateSelectedState() {
        // Only update if we have options loaded
        if (this._options.length > 0) {
            // Update state only if changes are needed
            const updatedOptions = updateItemsSelectedState(this._options, [this._value], 'selected');
            if (updatedOptions !== this._options) {
                this._options = updatedOptions;
            }
        }
    }
    render() {
        return html `<uui-select .options=${this._options} @change=${this.#onChange}></uui-select>`;
    }
};
__decorate([
    property()
], UmbPropertyEditorUISelectElement.prototype, "value", null);
__decorate([
    state()
], UmbPropertyEditorUISelectElement.prototype, "_options", void 0);
UmbPropertyEditorUISelectElement = __decorate([
    customElement('umb-property-editor-ui-select')
], UmbPropertyEditorUISelectElement);
export { UmbPropertyEditorUISelectElement };
export default UmbPropertyEditorUISelectElement;
