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';
import { convertToNumber, countDecimals } from '../../../../utils/number';
/**
 * Input numeric keyboard controller
 */
export class VegaInputNumericKeyboardController extends KeyboardManagerSlimmer {
  constructor() {
    super((target) => {
      return [
        {
          keyCode: ['ArrowUp', 'ArrowDown'],
          activeElementInScope: [target.host],
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: target.onArrowUpOrDown,
        },
        // Vega-input converts number inputs to text. When using the ArrowUp key in a text input,
        //   the cursor moves to the front. Since the value changes,
        //   vega-input formats the value and moves the cursor to the end, causing flickering.
        //   This behavior is prevented here.
        {
          keyCode: ['ArrowUp'],
          eventName: 'keydown',
          activeElementInScope: [target.host],
          /*
           * The below method is e2e-test covered in
           * @see{module:vega-input-numeric-increment-keyboard-e2e}
           */
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: /* istanbul ignore next */ (event) => {
            event.preventDefault();
          },
        },
      ];
    });
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-input-numeric-increment-keyboard-e2e}
     */
    /* istanbul ignore next */
    /**
     * Update the value while press ArrowUp or ArrowDown
     *
     * @param {KeyboardEvent} event - keyboard event
     */
    this.onArrowUpOrDown = (event) => {
      const updateValue = event.shiftKey ? this.majorIncrement : this.minorIncrement;
      const value = convertToNumber(this.value.replace(/,/g, '')) || 0;
      const decimalCount = Math.max(countDecimals(value), countDecimals(updateValue));
      if (event.code === 'ArrowUp') {
        this.valueController.updateValue(`${(value + updateValue).toFixed(decimalCount)}`);
      }
      else if (event.code === 'ArrowDown') {
        this.valueController.updateValue(`${(value - updateValue).toFixed(decimalCount)}`);
      }
    };
  }
}
__decorate([
  MapToComponentField()
], VegaInputNumericKeyboardController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericKeyboardController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericKeyboardController.prototype, "minorIncrement", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericKeyboardController.prototype, "majorIncrement", void 0);
__decorate([
  MapToComponentField()
], VegaInputNumericKeyboardController.prototype, "valueController", void 0);
