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 { UmbSectionPickerInputContext } from './input-section.context.js';
import { css, html, customElement, property, state } from '../../../../../external/lit/index.js';
import { UmbLitElement } from '../../../lit-element/index.js';
import { splitStringToArray } from '../../../utils/index.js';
import { UmbFormControlMixin } from '../../../validation/index.js';
let UmbInputSectionElement = class UmbInputSectionElement 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(uniques) {
        this.#pickerContext.setSelection(uniques);
    }
    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 UmbSectionPickerInputContext(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));
    }
    getFormElement() {
        return undefined;
    }
    render() {
        return html `
			<uui-ref-list>${this._items?.map((item) => this._renderItem(item))}</uui-ref-list>
			<uui-button
				id="btn-add"
				look="placeholder"
				@click=${() => this.#pickerContext.openPicker()}
				label=${this.localize.term('general_choose')}></uui-button>
		`;
    }
    _renderItem(item) {
        if (!item.unique)
            return;
        return html ` <umb-ref-section .item=${item}>
			<uui-action-bar slot="actions">
				<uui-button @click=${() => this.#pickerContext.requestRemoveItem(item.unique)} label="Remove ${item.name}"
					>Remove</uui-button
				>
			</uui-action-bar>
		</umb-ref-section>`;
    }
    static { this.styles = [
        css `
			#btn-add {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Number })
], UmbInputSectionElement.prototype, "min", null);
__decorate([
    property({ type: String, attribute: 'min-message' })
], UmbInputSectionElement.prototype, "minMessage", void 0);
__decorate([
    property({ type: Number })
], UmbInputSectionElement.prototype, "max", null);
__decorate([
    property({ type: String, attribute: 'min-message' })
], UmbInputSectionElement.prototype, "maxMessage", void 0);
__decorate([
    property({ type: String })
], UmbInputSectionElement.prototype, "value", null);
__decorate([
    state()
], UmbInputSectionElement.prototype, "_items", void 0);
UmbInputSectionElement = __decorate([
    customElement('umb-input-section')
], UmbInputSectionElement);
export { UmbInputSectionElement };
export default UmbInputSectionElement;
