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 { css, html, nothing, customElement, property, query, ifDefined, state, when, } from '../../../../external/lit/index.js';
import { umbConfirmModal } from '../../modal/index.js';
import { UmbChangeEvent, UmbInputEvent, UmbDeleteEvent } from '../../event/index.js';
import { UmbLitElement } from '../../lit-element/index.js';
import { UUIFormControlMixin } from '../../../../external/uui/index.js';
/**
 * @element umb-multiple-color-picker-item-input
 */
let UmbMultipleColorPickerItemInputElement = class UmbMultipleColorPickerItemInputElement extends UUIFormControlMixin(UmbLitElement, '') {
    constructor() {
        super(...arguments);
        this._valueHex = '';
        /**
         * Disables the input
         * @type {boolean}
         * @attr
         * @default false
         */
        this.disabled = false;
        /**
         * Disables the input
         * @type {boolean}
         * @attr
         * @default false
         */
        this.readonly = false;
        this.showLabels = false;
    }
    set value(value) {
        this._valueHex = this.#expandHex(value);
        if (value.startsWith('#')) {
            super.value = value.substring(1);
        }
        else {
            super.value = value;
        }
    }
    get value() {
        return super.value;
    }
    #expandHex(hex) {
        hex = hex.replace(/^#/, '');
        // If it's 3-digit, expand it
        if (hex.length === 3) {
            hex = hex
                .split('')
                .map((ch) => ch + ch)
                .join('');
        }
        return `#${hex}`;
    }
    async #onDelete() {
        await umbConfirmModal(this, {
            headline: `${this.localize.term('actions_delete')} ${this.value || ''}`,
            content: this.localize.term('content_nestedContentDeleteItem'),
            color: 'danger',
            confirmLabel: this.localize.term('actions_delete'),
        });
        this.dispatchEvent(new UmbDeleteEvent());
    }
    #onLabelInput(event) {
        event.stopPropagation();
        this.label = event.target.value;
        this.dispatchEvent(new UmbInputEvent());
    }
    #onLabelKeydown(event) {
        event.stopPropagation();
        const target = event.currentTarget;
        if (event.key === 'Enter' && target.value) {
            this.dispatchEvent(new CustomEvent('enter'));
        }
    }
    #onLabelChange(event) {
        event.stopPropagation();
        this.label = event.target.value;
        this.dispatchEvent(new UmbChangeEvent());
    }
    #onValueKeydown(event) {
        event.stopPropagation();
        if (event.key === 'Enter')
            this.#onColorClick();
    }
    #onValueChange(event) {
        event.stopPropagation();
        this.value = event.target.value;
        this.dispatchEvent(new UmbChangeEvent());
    }
    #onValueInput(event) {
        event.stopPropagation();
        this.value = event.target.value;
        this.dispatchEvent(new UmbInputEvent());
    }
    #onColorChange(event) {
        event.stopPropagation();
        this.value = this._colorPicker.value;
        this.dispatchEvent(new UmbChangeEvent());
    }
    // Prevent valid events from bubbling outside the message element
    #onValid(event) {
        event.stopPropagation();
    }
    // Prevent invalid events from bubbling outside the message element
    #onInvalid(event) {
        event.stopPropagation();
    }
    async focus() {
        await this.updateComplete;
        this._input?.focus();
    }
    getFormElement() {
        return undefined;
    }
    #onColorClick() {
        this._colorPicker.click();
    }
    render() {
        //TODO: Using native input=color element instead of uui-color-picker due to its huge size and bad adaptability as a pop up
        return html `
			<umb-form-validation-message id="validation-message" @invalid=${this.#onInvalid} @valid=${this.#onValid}>
				<div id="item">
					${this.disabled || this.readonly ? nothing : html `<uui-icon name="icon-grip" class="handle"></uui-icon>`}
					<div class="color-wrapper">
						<uui-input
							id="input"
							value=${this.value}
							label=${this.localize.term('general_value')}
							placeholder=${this.localize.term('general_value')}
							required=${this.required}
							required-message="Value is missing"
							@keydown=${this.#onValueKeydown}
							@input=${this.#onValueInput}
							@change=${this.#onValueChange}>
							<uui-color-swatch
								slot="prepend"
								label=${this.value}
								value=${this._valueHex}
								@click=${this.#onColorClick}></uui-color-swatch>
						</uui-input>
						<input
							type="color"
							id="color"
							value=${this._valueHex}
							aria-hidden="${true}"
							@change=${this.#onColorChange} />
					</div>
					${when(this.showLabels, () => html `
							<uui-input
								label=${this.localize.term('placeholders_label')}
								placeholder=${this.localize.term('placeholders_label')}
								value=${ifDefined(this.label)}
								@keydown=${this.#onLabelKeydown}
								@input="${this.#onLabelInput}"
								@change="${this.#onLabelChange}"
								?disabled=${this.disabled}
								?readonly=${this.readonly}></uui-input>
						`)}
					${when(!this.readonly, () => html `
							<uui-button
								compact
								label=${this.localize.term('actions_delete')}
								look="outline"
								?disabled=${this.disabled}
								@click=${this.#onDelete}>
								<uui-icon name="icon-trash"></uui-icon>
							</uui-button>
						`)}
				</div>
			</umb-form-validation-message>
		`;
    }
    static { this.styles = [
        css `
			:host {
				display: flex;
				align-items: center;
				margin-bottom: var(--uui-size-space-3);
				gap: var(--uui-size-space-3);
			}

			#item {
				position: relative;
				display: flex;
				gap: var(--uui-size-1);
				align-items: center;
			}
			uui-input {
				flex: 1;
			}

			uui-color-swatch {
				padding: var(--uui-size-1);
			}

			uui-color-swatch:focus-within {
				outline: 2px solid var(--uui-color-selected);
				outline-offset: 0;
				border-radius: var(--uui-border-radius);
			}

			.color-wrapper {
				position: relative;
				flex: 1;
				display: flex;
				flex-direction: column;
			}

			uui-input,
			#validation-message {
				flex: 1;
			}

			input[type='color'] {
				visibility: hidden;
				width: 0px;
				padding: 0;
				margin: 0;
				position: absolute;
			}

			.handle {
				cursor: grab;
			}

			.handle:active {
				cursor: grabbing;
			}
		`,
    ]; }
};
__decorate([
    property({ type: String })
], UmbMultipleColorPickerItemInputElement.prototype, "value", null);
__decorate([
    state()
], UmbMultipleColorPickerItemInputElement.prototype, "_valueHex", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], UmbMultipleColorPickerItemInputElement.prototype, "disabled", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], UmbMultipleColorPickerItemInputElement.prototype, "readonly", void 0);
__decorate([
    property({ type: String })
], UmbMultipleColorPickerItemInputElement.prototype, "label", void 0);
__decorate([
    query('#input')
], UmbMultipleColorPickerItemInputElement.prototype, "_input", void 0);
__decorate([
    query('#color')
], UmbMultipleColorPickerItemInputElement.prototype, "_colorPicker", void 0);
__decorate([
    property({ type: Boolean })
], UmbMultipleColorPickerItemInputElement.prototype, "showLabels", void 0);
UmbMultipleColorPickerItemInputElement = __decorate([
    customElement('umb-multiple-color-picker-item-input')
], UmbMultipleColorPickerItemInputElement);
export { UmbMultipleColorPickerItemInputElement };
export default UmbMultipleColorPickerItemInputElement;
