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 { UMB_DOCUMENT_ENTITY_TYPE } from '../../entity.js';
import { customElement, html, property, state } from '../../../../../external/lit/index.js';
import { UmbChangeEvent } from '../../../../core/event/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
import { UmbPropertyEditorUiInteractionMemoryManager } from '../../../../core/property-editor/index.js';
import { UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, UmbFormControlMixin } from '../../../../core/validation/index.js';
let UmbPropertyEditorUIDocumentPickerElement = class UmbPropertyEditorUIDocumentPickerElement extends UmbFormControlMixin(UmbLitElement) {
    set config(config) {
        this.#interactionMemoryManager.setPropertyEditorConfig(config);
        if (!config)
            return;
        const minMax = config.getValueByAlias('validationLimit');
        if (minMax) {
            this._min = minMax.min && minMax.min > 0 ? minMax.min : 0;
            this._max = minMax.max && minMax.max > 0 ? minMax.max : 1;
        }
        this._startNodeId = config.getValueByAlias('startNodeId');
    }
    #interactionMemoryManager;
    constructor() {
        super();
        /**
         * Sets the input to readonly mode, meaning value cannot be changed but still able to read and select its content.
         * @type {boolean}
         * @attr
         * @default false
         */
        this.readonly = false;
        this.mandatory = false;
        this.mandatoryMessage = UMB_VALIDATION_EMPTY_LOCALIZATION_KEY;
        this._min = 0;
        // NOTE: The legacy "Content Picker" property-editor only supported 1 item,
        // so that's why it's being enforced here. We'll evolve this in a future version. [LK]
        this._max = 1;
        this._interactionMemories = [];
        this.#interactionMemoryManager = new UmbPropertyEditorUiInteractionMemoryManager(this, {
            memoryUniquePrefix: 'UmbDocumentPicker',
        });
        this.observe(this.#interactionMemoryManager.memoriesForPropertyEditor, (interactionMemories) => {
            this._interactionMemories = interactionMemories ?? [];
        });
    }
    firstUpdated() {
        this.addFormControlElement(this.shadowRoot.querySelector('umb-input-document'));
    }
    #onChange(event) {
        this.value = event.target.value;
        this.dispatchEvent(new UmbChangeEvent());
    }
    async #onInputInteractionMemoriesChange(event) {
        const target = event.target;
        const interactionMemories = target.interactionMemories;
        if (interactionMemories && interactionMemories.length > 0) {
            await this.#interactionMemoryManager.saveMemoriesForPropertyEditor(interactionMemories);
        }
        else {
            await this.#interactionMemoryManager.deleteMemoriesForPropertyEditor();
        }
    }
    render() {
        const startNode = this._startNodeId
            ? { unique: this._startNodeId, entityType: UMB_DOCUMENT_ENTITY_TYPE }
            : undefined;
        return html `
			<umb-input-document
				.min=${this._min}
				.max=${this._max}
				.startNode=${startNode}
				.value=${this.value}
				@change=${this.#onChange}
				?readonly=${this.readonly}
				?required=${this.mandatory}
				.requiredMessage=${this.mandatoryMessage}
				.interactionMemories=${this._interactionMemories}
				@interaction-memories-change=${this.#onInputInteractionMemoriesChange}>
			</umb-input-document>
		`;
    }
};
__decorate([
    property({ type: Boolean, reflect: true })
], UmbPropertyEditorUIDocumentPickerElement.prototype, "readonly", void 0);
__decorate([
    property({ type: Boolean })
], UmbPropertyEditorUIDocumentPickerElement.prototype, "mandatory", void 0);
__decorate([
    property({ type: String })
], UmbPropertyEditorUIDocumentPickerElement.prototype, "mandatoryMessage", void 0);
__decorate([
    state()
], UmbPropertyEditorUIDocumentPickerElement.prototype, "_min", void 0);
__decorate([
    state()
], UmbPropertyEditorUIDocumentPickerElement.prototype, "_max", void 0);
__decorate([
    state()
], UmbPropertyEditorUIDocumentPickerElement.prototype, "_startNodeId", void 0);
__decorate([
    state()
], UmbPropertyEditorUIDocumentPickerElement.prototype, "_interactionMemories", void 0);
UmbPropertyEditorUIDocumentPickerElement = __decorate([
    customElement('umb-property-editor-ui-document-picker')
], UmbPropertyEditorUIDocumentPickerElement);
export { UmbPropertyEditorUIDocumentPickerElement };
export default UmbPropertyEditorUIDocumentPickerElement;
