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, customElement, html, ifDefined, nothing, property, state, styleMap, unsafeHTML, } from '../../../external/lit/index.js';
import { UmbLitElement } from '../../core/lit-element/index.js';
import { UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, UmbFormControlMixin } from '../../core/validation/index.js';
import { UmbTextStyles } from '../../core/style/index.js';
import { UmbChangeEvent } from '../../core/event/index.js';
import { getCharacterCountState, isCharacterLimitExceeded } from '../utils/character-count.js';
let UmbPropertyEditorUITextareaElement = class UmbPropertyEditorUITextareaElement extends UmbFormControlMixin(UmbLitElement, undefined) {
    constructor() {
        super(...arguments);
        /**
         * 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.mandatoryMessage = UMB_VALIDATION_EMPTY_LOCALIZATION_KEY;
        this._css = {};
    }
    set config(config) {
        this._maxChars = Number(config?.getValueByAlias('maxChars')) || undefined;
        this._rows = Number(config?.getValueByAlias('rows')) || undefined;
        // min/max height where for a short period present in the config, but we do not want this complexity of our configuration.
        // @deprecated remove config option in v.18, leave good default.
        const _minHeight = Number(config?.getValueByAlias('minHeight')) || undefined;
        const _maxHeight = Number(config?.getValueByAlias('maxHeight')) || undefined;
        this._css = {
            '--uui-textarea-min-height': _minHeight ? `${_minHeight}px` : 'reset',
            '--uui-textarea-max-height': _maxHeight ? `${_maxHeight}px` : '33vh',
        };
    }
    firstUpdated() {
        this.addFormControlElement(this.shadowRoot.querySelector('uui-textarea'));
    }
    focus() {
        return this.shadowRoot?.querySelector('uui-textarea')?.focus();
    }
    #getMaxLengthMessage(max, current) {
        const exceeded = current - max;
        return this.localize.term('textbox_characters_exceed', max, exceeded);
    }
    #onInput(event) {
        const newValue = event.target.value;
        if (newValue === this.value)
            return;
        this.value = newValue;
        if (isCharacterLimitExceeded(this._maxChars, newValue.length)) {
            const textarea = this.shadowRoot?.querySelector('uui-textarea');
            if (textarea) {
                textarea.pristine = false;
            }
            this.pristine = false;
        }
        this.dispatchEvent(new UmbChangeEvent());
    }
    #renderCharacterCount() {
        if (!this._maxChars || this.readonly)
            return nothing;
        const { remaining, visible } = getCharacterCountState(this._maxChars, this.value?.length ?? 0);
        if (!visible)
            return nothing;
        return html `<div class="char-count">${unsafeHTML(this.localize.term('textbox_characters_left', remaining))}</div>`;
    }
    render() {
        return html `
			<uui-textarea
				.label=${this.localize.term('general_fieldFor', [this.name])}
				data-mark="input:textarea"
				style=${styleMap(this._css)}
				.autoHeight=${this._rows ? false : true}
				maxlength=${ifDefined(this._maxChars)}
				rows=${ifDefined(this._rows)}
				.value=${this.value ?? ''}
				@input=${this.#onInput}
				?required=${this.mandatory}
				.requiredMessage=${this.mandatoryMessage}
				.maxlengthMessage=${this.#getMaxLengthMessage.bind(this)}
				?readonly=${this.readonly}></uui-textarea>
			${this.#renderCharacterCount()}
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			uui-textarea {
				width: 100%;
			}

			.char-count {
				color: var(--uui-color-text-alt);
			}

			:host(:not(:focus-within)) .char-count {
				display: none;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Boolean, reflect: true })
], UmbPropertyEditorUITextareaElement.prototype, "readonly", void 0);
__decorate([
    property({ type: Boolean })
], UmbPropertyEditorUITextareaElement.prototype, "mandatory", void 0);
__decorate([
    property({ type: String })
], UmbPropertyEditorUITextareaElement.prototype, "mandatoryMessage", void 0);
__decorate([
    property({ type: String })
], UmbPropertyEditorUITextareaElement.prototype, "name", void 0);
__decorate([
    state()
], UmbPropertyEditorUITextareaElement.prototype, "_maxChars", void 0);
__decorate([
    state()
], UmbPropertyEditorUITextareaElement.prototype, "_rows", void 0);
__decorate([
    state()
], UmbPropertyEditorUITextareaElement.prototype, "_css", void 0);
UmbPropertyEditorUITextareaElement = __decorate([
    customElement('umb-property-editor-ui-textarea')
], UmbPropertyEditorUITextareaElement);
export { UmbPropertyEditorUITextareaElement };
export default UmbPropertyEditorUITextareaElement;
