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 } from 'vega-slimmer/core';
import { KeyboardManagerSlimmer } from '../../../../helpers/slimmers/keyboard-manager-slimmer';
/**
 * Input passcode keyboard controller
 */
export class VegaInputPasscodeKeyboardController extends KeyboardManagerSlimmer {
  constructor() {
    super((target) => [
      {
        keyCode: 'Backspace',
        activeElementInScope: [this.inputPasscodeRenderer.getInputPasscodeContainerRef()],
        eventName: 'keydown',
        callback: 
        /* istanbul ignore next */
        /* eslint-disable jsdoc/require-jsdoc */
        (event) => {
          target.deletePasscode(event);
        },
      },
      {
        keyCode: ['ArrowLeft', 'ArrowRight'],
        activeElementInScope: [this.inputPasscodeRenderer.getInputPasscodeContainerRef()],
        eventName: 'keydown',
        callback: 
        /* istanbul ignore next */
        /* eslint-disable jsdoc/require-jsdoc */
        (event) => {
          target.controlDirectionByKeyboard(event);
        },
      },
    ]);
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-input-passcode-on-input-value-delete}
     */
    /* istanbul ignore next */
    /**
     * handle logic of delete input passcode values
     *
     * @param {KeyboardEvent} event - the keyboard event of delete
     */
    this.deletePasscode = (event) => {
      event.preventDefault();
      const currentIndex = this.inputPasscodeRenderer.getCurrentIndex();
      if (this.vegaInputValues.get(currentIndex)) {
        this.vegaInputValues.withMutation((arr) => {
          for (let index = currentIndex; index < this.length - 1; index++) {
            arr[index] = arr[index + 1];
          }
          arr[this.length - 1] = '';
        });
      }
      else if (!this.vegaInputValues.get(currentIndex) && currentIndex > 0) {
        this.vegaInputValues.set(currentIndex - 1, '');
        void this.inputPasscodeRenderer.getVegaInputRefs()[currentIndex - 1].doFocus();
      }
    };
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-input-passcode-on-arrow-keyboard-press}
   */
  /* istanbul ignore next */
  /**
   * handle logic of directions of ArrowLeft and ArrowRight
   *
   * @param {KeyboardEvent} event - the keyboard event of ArrowLeft and ArrowRight
   */
  controlDirectionByKeyboard(event) {
    event.preventDefault();
    const currentIndex = this.inputPasscodeRenderer.getCurrentIndex();
    const vegaInputRefs = this.inputPasscodeRenderer.getVegaInputRefs();
    if (event.key === 'ArrowLeft' && currentIndex > 0) {
      void vegaInputRefs[currentIndex - 1].doFocus();
    }
    if (event.key === 'ArrowRight' &&
      this.vegaInputValues.get(currentIndex) &&
      currentIndex < this.length - 1) {
      void vegaInputRefs[currentIndex + 1].doFocus();
    }
  }
}
__decorate([
  MapToComponentField()
], VegaInputPasscodeKeyboardController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeKeyboardController.prototype, "length", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeKeyboardController.prototype, "vegaInputValues", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeKeyboardController.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], VegaInputPasscodeKeyboardController.prototype, "inputPasscodeRenderer", void 0);
