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, property, state } from '../../../external/lit/index.js';
import { UmbChangeEvent } from '../../core/event/index.js';
import { UmbLitElement } from '../../core/lit-element/index.js';
import { UMB_PROPERTY_CONTEXT } from '../../core/property/index.js';
import { UMB_VALIDATION_EMPTY_LOCALIZATION_KEY, UmbFormControlMixin } from '../../core/validation/index.js';
let UmbPropertyEditorUINumberElement = class UmbPropertyEditorUINumberElement extends UmbFormControlMixin(UmbLitElement, undefined) {
    set config(config) {
        if (!config)
            return;
        this._min = this.#parseNumber(config.getValueByAlias('min'));
        this._max = this.#parseNumber(config.getValueByAlias('max'));
        this._step = this.#parseNumber(config.getValueByAlias('step'));
        this._placeholder = this.localize.string(config.getValueByAlias('placeholder') ?? '');
    }
    constructor() {
        super();
        /**
         * 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.consumeContext(UMB_PROPERTY_CONTEXT, (context) => {
            this._label = context?.getLabel();
        });
        this.addValidator('rangeUnderflow', () => this.localize.term('validation_numberMinimum', this._min), () => !!this._min && this.value < this._min);
        this.addValidator('rangeOverflow', () => this.localize.term('validation_numberMaximum', this._max), () => !!this._max && this.value > this._max);
        this.addValidator('customError', () => this.localize.term('validation_numberMisconfigured', this._min, this._max), () => !!this._min && !!this._max && this._min > this._max);
    }
    firstUpdated() {
        if (this._min && this._max && this._min > this._max) {
            console.warn(`Property '${this._label}' (Numeric) has been misconfigured, 'min' is greater than 'max'. Please correct your data type configuration.`, this);
        }
        this.addFormControlElement(this.shadowRoot.querySelector('uui-input'));
    }
    #parseNumber(input) {
        const num = Number(input);
        return Number.isFinite(num) ? num : undefined;
    }
    #onChange(event) {
        const newValue = event.target.value === '' ? undefined : this.#parseNumber(event.target.value);
        if (newValue === this.value)
            return;
        this.value = newValue;
        this.dispatchEvent(new UmbChangeEvent());
    }
    render() {
        return html `
			<uui-input
				type="number"
				label=${ifDefined(this._label)}
				min=${ifDefined(this._min)}
				max=${ifDefined(this._max)}
				step=${ifDefined(this._step)}
				value=${this.value?.toString() ?? ''}
				.placeholder=${this._placeholder ?? ''}
				.requiredMessage=${this.mandatoryMessage}
				?required=${this.mandatory}
				?readonly=${this.readonly}
				@change=${this.#onChange}>
			</uui-input>
		`;
    }
    static { this.styles = [
        css `
			uui-input {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    property({ type: Boolean, reflect: true })
], UmbPropertyEditorUINumberElement.prototype, "readonly", void 0);
__decorate([
    property({ type: Boolean })
], UmbPropertyEditorUINumberElement.prototype, "mandatory", void 0);
__decorate([
    property({ type: String })
], UmbPropertyEditorUINumberElement.prototype, "mandatoryMessage", void 0);
__decorate([
    state()
], UmbPropertyEditorUINumberElement.prototype, "_label", void 0);
__decorate([
    state()
], UmbPropertyEditorUINumberElement.prototype, "_max", void 0);
__decorate([
    state()
], UmbPropertyEditorUINumberElement.prototype, "_min", void 0);
__decorate([
    state()
], UmbPropertyEditorUINumberElement.prototype, "_step", void 0);
__decorate([
    state()
], UmbPropertyEditorUINumberElement.prototype, "_placeholder", void 0);
UmbPropertyEditorUINumberElement = __decorate([
    customElement('umb-property-editor-ui-number')
], UmbPropertyEditorUINumberElement);
export { UmbPropertyEditorUINumberElement };
export default UmbPropertyEditorUINumberElement;
