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 { UmbMemberTypePickerInputContext } from './input-member-type.context.js';
import { css, customElement, html, nothing, property, repeat, state, when } 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 '../../../../core/entity-item/index.js';
let UmbInputMemberTypeElement = class UmbInputMemberTypeElement extends UmbFormControlMixin(UmbLitElement) {
    /**
     * This is a minimum amount of selected items 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 items in this input.
     * @type {number}
     * @attr
     * @default
     */
    set max(value) {
        this.#pickerContext.max = value;
    }
    get max() {
        return this.#pickerContext.max;
    }
    set selection(ids) {
        this.#pickerContext.setSelection(ids);
    }
    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();
        /**
         * Min validation message.
         * @type {boolean}
         * @attr
         * @default
         */
        this.minMessage = 'This field need more items';
        /**
         * Max validation message.
         * @type {boolean}
         * @attr
         * @default
         */
        this.maxMessage = 'This field exceeds the allowed amount of items';
        this.#pickerContext = new UmbMemberTypePickerInputContext(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), '_observeStatuses');
    }
    getFormElement() {
        return undefined;
    }
    #getPickableFilter() {
        return (x) => !x.isFolder;
    }
    #openPicker() {
        this.#pickerContext.openPicker({
            hideTreeRoot: true,
            pickableFilter: this.#getPickableFilter(),
        });
    }
    render() {
        return html ` ${this.#renderItems()} ${this.#renderAddButton()} `;
    }
    #renderItems() {
        if (!this._statuses)
            return;
        return html `
			<uui-ref-list>
				${repeat(this._statuses, (status) => status.unique, (status) => this.#renderItem(status))}
			</uui-ref-list>
		`;
    }
    #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')}"
				>${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';
        // For error state, use umb-entity-item-ref
        if (isError) {
            return html `
				<umb-entity-item-ref
					id=${unique}
					?error=${true}
					.errorMessage=${status.state.error}
					.errorDetail=${unique}
					?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>
			`;
        }
        // For successful items, use the member type specific component
        if (!item?.unique)
            return nothing;
        return html `
			<uui-ref-node-document-type name=${this.localize.string(item.name)}>
				${when(item.icon, () => html `<umb-icon slot="icon" name=${item.icon}></umb-icon>`)}
				<uui-action-bar slot="actions">
					<uui-button
						@click=${() => this.#pickerContext.requestRemoveItem(unique)}
						label="Remove Member Type ${item.name}"
						>${this.localize.term('general_remove')}</uui-button
					>
				</uui-action-bar>
			</uui-ref-node-document-type>
		`;
    }
    static { this.styles = [
        css `
			#btn-add {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Number })
], UmbInputMemberTypeElement.prototype, "min", null);
__decorate([
    property({ type: String, attribute: 'min-message' })
], UmbInputMemberTypeElement.prototype, "minMessage", void 0);
__decorate([
    property({ type: Number })
], UmbInputMemberTypeElement.prototype, "max", null);
__decorate([
    property({ type: String, attribute: 'min-message' })
], UmbInputMemberTypeElement.prototype, "maxMessage", void 0);
__decorate([
    property({ type: String })
], UmbInputMemberTypeElement.prototype, "value", null);
__decorate([
    state()
], UmbInputMemberTypeElement.prototype, "_items", void 0);
__decorate([
    state()
], UmbInputMemberTypeElement.prototype, "_statuses", void 0);
UmbInputMemberTypeElement = __decorate([
    customElement('umb-input-member-type')
], UmbInputMemberTypeElement);
export { UmbInputMemberTypeElement };
export default UmbInputMemberTypeElement;
