import { useCallback } from 'react';
import { Decimal } from 'decimal.js';
import { cloneDeep, set } from 'lodash';
var KEY_ARROW_UP = 'ArrowUp';
var KEY_ARROW_DOWN = 'ArrowDown';
export var useInputKeys = function useInputKeys(step, onChange) {
  var handleKeyDown = useCallback(function (event) {
    var key = event.key,
      value = event.currentTarget.value;
    if (![KEY_ARROW_UP, KEY_ARROW_DOWN].includes(key)) {
      return;
    }
    event.preventDefault();
    if (value && !Number.isFinite(parseFloat(value))) {
      return;
    }
    var decimal = new Decimal(value || 0);
    var newEvent = cloneDeep(event);
    set(newEvent, 'currentTarget.value', String(key === KEY_ARROW_UP ? decimal.plus(step) : decimal.minus(step)));
    onChange(newEvent);
  }, [step, onChange]);
  return {
    handleKeyDown: handleKeyDown
  };
};