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 { html, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import { registerComponent } from '../common/definitions/register.js';
import { createFormValueState } from '../common/mixins/forms/form-value.js';
import { partMap } from '../common/part-map.js';
import { isEmpty } from '../common/util.js';
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
import { IgcMaskInputBaseComponent, } from './mask-input-base.js';
import { maskValidators } from './validators.js';
export default class IgcMaskInputComponent extends IgcMaskInputBaseComponent {
    constructor() {
        super(...arguments);
        this._formValue = createFormValueState(this, {
            initialValue: '',
            transformers: {
                setFormValue: (value) => this._isRawMode ? value || null : this._maskedValue || null,
            },
        });
        this.valueMode = 'raw';
    }
    static { this.tagName = 'igc-mask-input'; }
    static register() {
        registerComponent(IgcMaskInputComponent, IgcValidationContainerComponent);
    }
    get __validators() {
        return maskValidators;
    }
    get _isRawMode() {
        return this.valueMode === 'raw';
    }
    set value(string) {
        const value = string ?? '';
        this._maskedValue = this._parser.apply(value);
        this._updateMaskedValue();
        this._formValue.setValueAndFormState(value);
    }
    get value() {
        const value = this._formValue.value;
        if (this._isRawMode) {
            return value;
        }
        return value ? this._maskedValue : value;
    }
    set mask(value) {
        super.mask = value;
        if (this.value) {
            this._maskedValue = this._parser.apply(this._formValue.value);
        }
    }
    get mask() {
        return super.mask;
    }
    set prompt(value) {
        super.prompt = value;
        if (this.value) {
            this._maskedValue = this._parser.apply(this._formValue.value);
        }
    }
    get prompt() {
        return super.prompt;
    }
    _handleDragEnter() {
        if (!this._focused && !this._formValue.value) {
            this._maskedValue = this._parser.emptyMask;
        }
    }
    _handleDragLeave() {
        if (!this._focused) {
            this._updateMaskedValue();
        }
    }
    async _handleFocus() {
        this._focused = true;
        if (this.readOnly) {
            return;
        }
        if (!this._formValue.value) {
            this._maskedValue = this._parser.emptyMask;
            await this.updateComplete;
            this.select();
        }
    }
    _handleBlur() {
        this._focused = false;
        this._updateMaskedValue();
        super._handleBlur();
    }
    _handleChange() {
        this._setTouchedState();
        this.emitEvent('igcChange', { detail: this.value });
    }
    _restoreDefaultValue() {
        const value = this.defaultValue;
        this._maskedValue = this._parser.apply(value);
        this._updateMaskedValue();
        this._formValue.setValueAndFormState(value);
    }
    async _updateInput(text, { start, end }) {
        const result = this._parser.replace(this._maskedValue, text, start, end);
        this._maskedValue = result.value;
        this._formValue.setValueAndFormState(this._parser.parse(this._maskedValue));
        this.requestUpdate();
        if (start !== this.mask.length) {
            this.emitEvent('igcInput', { detail: this.value });
        }
        await this.updateComplete;
        this.input.setSelectionRange(result.end, result.end);
    }
    _updateSetRangeTextValue() {
        this.value = this._parser.parse(this._maskedValue);
    }
    _updateMaskedValue() {
        if (this._maskedValue === this._parser.emptyMask) {
            this._maskedValue = '';
        }
    }
    renderInput() {
        return html `
      <input
        type="text"
        part=${partMap(this.resolvePartNames('input'))}
        name=${ifDefined(this.name)}
        .value=${live(this._maskedValue)}
        .placeholder=${this.placeholder ?? this._parser.escapedMask}
        ?readonly=${this.readOnly}
        ?disabled=${this.disabled}
        @dragenter=${this._handleDragEnter}
        @dragleave=${this._handleDragLeave}
        @dragstart=${this._setMaskSelection}
        @blur=${this._handleBlur}
        @focus=${this._handleFocus}
        @cut=${this._setMaskSelection}
        @change=${this._handleChange}
        @click=${this._handleClick}
        @compositionstart=${this._handleCompositionStart}
        @compositionend=${this._handleCompositionEnd}
        @input=${this._handleInput}
        aria-describedby=${ifDefined(isEmpty(this._helperText) ? nothing : 'helper-text')}
        @keydown=${this._setMaskSelection}
      />
    `;
    }
}
__decorate([
    property({ attribute: 'value-mode' })
], IgcMaskInputComponent.prototype, "valueMode", void 0);
__decorate([
    property()
], IgcMaskInputComponent.prototype, "value", null);
__decorate([
    property()
], IgcMaskInputComponent.prototype, "mask", null);
__decorate([
    property()
], IgcMaskInputComponent.prototype, "prompt", null);
//# sourceMappingURL=mask-input.js.map