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 { UmbStaticFilePickerInputContext } from './input-static-file.context.js';
import { css, customElement, html, nothing, property, repeat, state } from '../../../../external/lit/index.js';
import { splitStringToArray } from '../../../core/utils/index.js';
import { UmbFormControlMixin } from '../../../core/validation/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { UmbServerFilePathUniqueSerializer } from '../../../core/server-file-system/index.js';
import '../../../core/entity-item/index.js';
let UmbInputStaticFileElement = class UmbInputStaticFileElement extends UmbFormControlMixin(UmbLitElement) {
    #serializer;
    /**
     * This is a minimum amount of selected files in this input.
     * @type {number}
     * @attr
     * @default
     */
    set min(value) {
        this.#pickerContext.min = value;
    }
    get min() {
        return this.#pickerContext.min;
    }
    /**
     * This is a maximum amount of selected files in this input.
     * @type {number}
     * @attr
     * @default
     */
    set max(value) {
        this.#pickerContext.max = value;
    }
    get max() {
        return this.#pickerContext.max;
    }
    set selection(paths) {
        this.#pickerContext.setSelection(paths);
    }
    get selection() {
        return this.#pickerContext.getSelection();
    }
    set value(selectionString) {
        this.selection = splitStringToArray(selectionString);
    }
    get value() {
        return this.selection.length > 0 ? this.selection.join(',') : undefined;
    }
    #pickerContext;
    constructor() {
        super();
        this.#serializer = new UmbServerFilePathUniqueSerializer();
        /**
         * Min validation message.
         * @type {boolean}
         * @attr
         * @default
         */
        this.minMessage = 'This field need more files';
        /**
         * Max validation message.
         * @type {boolean}
         * @attr
         * @default
         */
        this.maxMessage = 'This field exceeds the allowed amount of files';
        this.#pickerContext = new UmbStaticFilePickerInputContext(this);
        this.addValidator('rangeUnderflow', () => this.minMessage, () => !!this.min && this.#pickerContext.getSelection().length < this.min);
        this.addValidator('rangeOverflow', () => this.maxMessage, () => !!this.max && this.#pickerContext.getSelection().length > this.max);
        this.observe(this.#pickerContext.selection, (selection) => (this.value = selection.join(',')));
        this.observe(this.#pickerContext.selectedItems, (selectedItems) => (this._items = selectedItems));
        this.observe(this.#pickerContext.statuses, (statuses) => (this._statuses = statuses));
    }
    getFormElement() {
        return undefined;
    }
    render() {
        if (!this._statuses)
            return nothing;
        return html `
			<uui-ref-list>
				${repeat(this._statuses, (status) => status.unique, (status) => this.#renderItem(status))}
			</uui-ref-list>
			${this.#renderAddButton()}
		`;
    }
    #openPicker() {
        this.#pickerContext.openPicker({
            pickableFilter: this.pickableFilter,
            multiple: this.max === 1 ? false : true,
            hideTreeRoot: true,
        });
    }
    #renderAddButton() {
        if (this.max === 1 && this.selection.length >= this.max)
            return;
        return html `
			<uui-button
				id="btn-add"
				look="placeholder"
				@click=${this.#openPicker}
				label=${this.localize.term('general_choose')}></uui-button>
		`;
    }
    #renderItem(status) {
        const unique = status.unique;
        const item = this._items?.find((x) => x.unique === unique);
        const isError = status.state.type === 'error';
        return html `
			<umb-entity-item-ref
				id=${unique}
				.item=${item}
				?error=${isError}
				.errorMessage=${status.state.error}
				.errorDetail=${isError ? this.#serializer.toServerPath(unique) : undefined}
				?standalone=${this.max === 1}>
				<uui-action-bar slot="actions">
					<uui-button
						label=${this.localize.term('general_remove')}
						@click=${() => this.#pickerContext.requestRemoveItem(unique)}></uui-button>
				</uui-action-bar>
			</umb-entity-item-ref>
		`;
    }
    static { this.styles = [
        css `
			#btn-add {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Number })
], UmbInputStaticFileElement.prototype, "min", null);
__decorate([
    property({ type: String, attribute: 'min-message' })
], UmbInputStaticFileElement.prototype, "minMessage", void 0);
__decorate([
    property({ type: Number })
], UmbInputStaticFileElement.prototype, "max", null);
__decorate([
    property({ type: String, attribute: 'min-message' })
], UmbInputStaticFileElement.prototype, "maxMessage", void 0);
__decorate([
    property({ type: String })
], UmbInputStaticFileElement.prototype, "value", null);
__decorate([
    property()
], UmbInputStaticFileElement.prototype, "pickableFilter", void 0);
__decorate([
    state()
], UmbInputStaticFileElement.prototype, "_items", void 0);
__decorate([
    state()
], UmbInputStaticFileElement.prototype, "_statuses", void 0);
UmbInputStaticFileElement = __decorate([
    customElement('umb-input-static-file')
], UmbInputStaticFileElement);
export { UmbInputStaticFileElement };
export default UmbInputStaticFileElement;
