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 { classMap, css, customElement, html, nothing, property, repeat } from '../../../../../external/lit/index.js';
import { UmbChangeEvent } from '../../../../core/event/index.js';
import { UmbLitElement } from '../../../../core/lit-element/index.js';
import { UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, UmbFormControlMixin } from '../../../../core/validation/index.js';
let UmbInputCheckboxListElement = class UmbInputCheckboxListElement extends UmbFormControlMixin(UmbLitElement, undefined) {
    #selection;
    set selection(values) {
        this.#selection = values;
        super.value = values.join(',');
    }
    get selection() {
        return this.#selection;
    }
    set value(value) {
        this.selection = value.split(',');
    }
    get value() {
        return this.selection.join(',');
    }
    constructor() {
        super();
        this.list = [];
        this.#selection = [];
        /**
         * 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.addValidator('valueMissing', () => this.requiredMessage ?? UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, () => !this.readonly && !!this.required && (this.value === undefined || this.value === null || this.value === ''));
    }
    #onChange(event) {
        event.stopPropagation();
        if (event.target.checked)
            this.selection = [...this.selection, event.target.value];
        else
            this.#removeFromSelection(this.selection.findIndex((value) => event.target.value === value));
        this.dispatchEvent(new UmbChangeEvent());
    }
    #removeFromSelection(index) {
        if (index == -1)
            return;
        const values = [...this.selection];
        values.splice(index, 1);
        this.selection = values;
    }
    render() {
        if (!this.list)
            return nothing;
        return html `
			<form>
				<uui-form @change=${this.#onChange}>
					${repeat(this.list, (item) => item.value, (item) => this.#renderCheckbox(item))}
				</uui-form>
			</form>
		`;
    }
    #renderCheckbox(item) {
        return html `
			<uui-checkbox
				class=${classMap({ invalid: !!item.invalid })}
				label=${item.label + (item.invalid ? ` (${this.localize.term('validation_legacyOption')})` : '')}
				title=${item.invalid ? this.localize.term('validation_legacyOptionDescription') : ''}
				value=${item.value}
				?checked=${item.checked}
				?readonly=${this.readonly}></uui-checkbox>
		`;
    }
    static { this.styles = [
        css `
			uui-checkbox {
				width: 100%;

				&.invalid {
					text-decoration: line-through;
				}
			}
		`,
    ]; }
};
__decorate([
    property({ attribute: false })
], UmbInputCheckboxListElement.prototype, "list", void 0);
__decorate([
    property({ type: Array })
], UmbInputCheckboxListElement.prototype, "selection", null);
__decorate([
    property()
], UmbInputCheckboxListElement.prototype, "value", null);
__decorate([
    property({ type: Boolean, reflect: true })
], UmbInputCheckboxListElement.prototype, "readonly", void 0);
__decorate([
    property({ type: Boolean })
], UmbInputCheckboxListElement.prototype, "required", void 0);
__decorate([
    property({ type: String })
], UmbInputCheckboxListElement.prototype, "requiredMessage", void 0);
UmbInputCheckboxListElement = __decorate([
    customElement('umb-input-checkbox-list')
], UmbInputCheckboxListElement);
export { UmbInputCheckboxListElement };
export default UmbInputCheckboxListElement;
