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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaBlur, VegaChange } from '../../../../helpers/event-manager/event-id/vega-event-id';
import { normalizeNumberByRanger } from '../../../../utils/number';
export class VegaInputNumericRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.onInputChange = (payload) => {
      this.valueController.updateValue(payload.detail);
    };
    this.onInputBlur = (payload) => {
      const vegaInput = payload.host;
      const normalizedValue = vegaInput.value.replace(/,/g, '');
      if (normalizedValue !== '' && !isNaN(normalizedValue)) {
        let inputValue = normalizeNumberByRanger(Number(normalizedValue), this.min, this.max);
        if (this.integerOnly) {
          if (inputValue === this.min) {
            inputValue = Math.ceil(this.min);
          }
          else if (inputValue === this.max) {
            inputValue = Math.floor(this.max);
          }
        }
        if (inputValue !== Number(normalizedValue)) {
          this.valueController.updateValue(inputValue.toString());
        }
      }
    };
  }
  getInputRef() {
    return this.vegaInputRef.shadowRoot.querySelector('input');
  }
  getContainerRef() {
    return this.vegaInputRef.shadowRoot.querySelector('.vega-input-container');
  }
  render() {
    const maskOptions = {
      thousandComma: this.thousandComma,
    };
    if (this.integerOnly) {
      maskOptions.precision = 0;
    }
    return (h("vega-input", { value: this.value || '', showClearIcon: this.showClearIcon, disabled: this.disabled, label: this.label, hint: this.hint, required: this.required, type: "number", placeholder: this.placeholder, size: this.size === 'small' ? 'small' : 'default', prefixIcon: this.prefixIcon, ref: (ref) => {
        this.vegaInputRef = ref;
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaChange, (payload) => this.onInputChange(payload));
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaBlur, (payload) => this.onInputBlur(payload));
      }, maskConfig: {
        type: 'number',
        options: maskOptions,
      } }));
  }
}
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "prefixIcon", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "showClearIcon", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "size", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "label", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "hint", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "required", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "thousandComma", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "integerOnly", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "min", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "max", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericRenderer.prototype, "valueController", void 0);
