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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { FORM_ELEMENT_VALUE_CHANGE } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
/**
 * Input passcode value controller
 */
export class VegaInputPasscodeValueController extends VegaSlimmer {
  /** initialize input values on load */
  componentWillLoad() {
    this.setValuesArray(this.value);
    this.value = this.vegaInputValues.join('');
  }
  /**
   * The `watchValue` function emits a change event with the current value and notifies the
   * `ChangeManager` about a form element value change.
   */
  watchValue() {
    this.setValuesArray(this.value);
    this.changeEventEmitter.emit(this.value);
    ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
    if (this.value.length === this.length) {
      this.completeEventEmitter.emit(this.value);
    }
  }
  /**
   * Synchronizes the rendered input elements with the controller's internal passcode values.
   */
  watchVegaInputValues() {
    for (let index = 0; index < this.length; index++) {
      if (this.inputPasscodeRenderer.getVegaInputRefs()[index]) {
        this.inputPasscodeRenderer.getVegaInputRefs()[index].value =
          this.vegaInputValues.get(index) || '';
      }
    }
    this.value = this.vegaInputValues.join('');
  }
  /**
   * initialize input values
   *
   * @param {string} value - the value to be initialized
   */
  setValuesArray(value) {
    const newValueArray = this.valueValidationFilter(value).slice(0, this.length).split('');
    this.vegaInputValues.setAll(newValueArray);
  }
  /**
   * Filters an input value according to the controller's input type.
   *
   * @param {string} value - The raw input string to validate and filter.
   * @returns {string} - The filtered string: white space free in text type, or digits-only otherwise.
   */
  valueValidationFilter(value) {
    return this.inputType === 'text' ? value.replace(/\s/g, '') : value.replace(/\D/g, '');
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-input-passcode-on-click-empty-input-field}
   */
  /* istanbul ignore next */
  /**
   * Searches through the input values and returns the index
   * of the first empty input.
   * Returns -1 if all inputs are filled.
   *
   * @returns {number} - the index of the first empty input field
   */
  getFirstEmptyIndex() {
    var _a;
    const { length } = this;
    const values = Array.from({ length }, (_, i) => this.vegaInputValues.get(i) || '');
    return (_a = values.findIndex((value) => !value || value === '')) !== null && _a !== void 0 ? _a : -1;
  }
}
__decorate([
  MapToComponentField()
], VegaInputPasscodeValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputPasscodeValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputPasscodeValueController.prototype, "vegaInputValues", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeValueController.prototype, "length", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeValueController.prototype, "inputType", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeValueController.prototype, "inputPasscodeRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeValueController.prototype, "completeEventEmitter", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaInputPasscodeValueController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchValue')
], VegaInputPasscodeValueController.prototype, "watchValue", null);
__decorate([
  MapToComponentMethod('watchVegaInputValues')
], VegaInputPasscodeValueController.prototype, "watchVegaInputValues", null);
