"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useInputNumber = void 0;
var _react = require("react");
var _utils = require("../utils");
var _currencyUtils = require("../utils/currencyUtils");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var useInputNumber = exports.useInputNumber = function useInputNumber(_ref, inputRef) {
  var _ref$decimalSeparator = _ref.decimalSeparator,
    decimalSeparator = _ref$decimalSeparator === void 0 ? "." : _ref$decimalSeparator,
    _ref$thousandSeparato = _ref.thousandSeparator,
    thousandSeparator = _ref$thousandSeparato === void 0 ? "," : _ref$thousandSeparato,
    disableAbbreviation = _ref.disableAbbreviation,
    disableMacros = _ref.disableMacros,
    decimalLength = _ref.decimalLength,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? "" : _ref$value,
    onChange = _ref.onChange,
    onSave = _ref.onSave,
    onEnterKeyPress = _ref.onEnterKeyPress,
    onClick = _ref.onClick,
    onCancel = _ref.onCancel,
    allowEmptyValue = _ref.allowEmptyValue,
    _ref$step = _ref.step,
    step = _ref$step === void 0 ? 1 : _ref$step,
    _ref$trimDecimals = _ref.trimDecimals,
    trimDecimals = _ref$trimDecimals === void 0 ? true : _ref$trimDecimals,
    limit = _ref.limit,
    _ref$validation = _ref.validation,
    validation = _ref$validation === void 0 ? _utils.validateNumberInput : _ref$validation,
    min = _ref.min,
    max = _ref.max,
    onBlur = _ref.onBlur;
  var isMaxValid = max === undefined || min === undefined || Number(max) >= Number(min);
  if (!isMaxValid) {
    console.warn("Warning: The maximum value is set to be lower than the minimum value. The maximum value will be ignored.");
  }
  var _useState = (0, _react.useState)(function () {
      return (0, _currencyUtils.formatNumber)(value, thousandSeparator, decimalSeparator, trimDecimals, decimalLength, disableAbbreviation ? "long" : "short");
    }),
    _useState2 = _slicedToArray(_useState, 2),
    currentValue = _useState2[0],
    setCurrentValue = _useState2[1];
  var _useState3 = (0, _react.useState)(function () {
      return (0, _currencyUtils.formatNumber)(value, thousandSeparator, decimalSeparator, trimDecimals, decimalLength, disableAbbreviation ? "long" : "short");
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    prevValue = _useState4[0],
    setPrevValue = _useState4[1];
  var _useState5 = (0, _react.useState)(function () {
      return (0, _currencyUtils.formatNumber)(value, "", decimalSeparator, false, decimalLength, "long");
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    unformattedValue = _useState6[0],
    setUnformattedValue = _useState6[1];
  var _useState7 = (0, _react.useState)(function () {
      return (0, _currencyUtils.formatNumber)(value, "", decimalSeparator, false, decimalLength, "long");
    }),
    _useState8 = _slicedToArray(_useState7, 2),
    unformattedPrevValue = _useState8[0],
    setUnformattedPrevValue = _useState8[1];
  (0, _react.useEffect)(function () {
    if (value !== prevValue && document.activeElement !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
      setCurrentValue((0, _currencyUtils.formatNumber)(value, thousandSeparator, decimalSeparator, trimDecimals, decimalLength, disableAbbreviation ? "long" : "short"));
      setPrevValue((0, _currencyUtils.formatNumber)(value, thousandSeparator, decimalSeparator, trimDecimals, decimalLength, disableAbbreviation ? "long" : "short"));
      setUnformattedValue((0, _currencyUtils.formatNumber)(value, "", decimalSeparator, false, decimalLength, "long"));
      setUnformattedPrevValue((0, _currencyUtils.formatNumber)(value, "", decimalSeparator, false, decimalLength, "long"));
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [disableAbbreviation, thousandSeparator, decimalSeparator, decimalLength, trimDecimals, value]);
  var _useState9 = (0, _react.useState)(false),
    _useState10 = _slicedToArray(_useState9, 2),
    focused = _useState10[0],
    setFocused = _useState10[1];
  var escapeRef = (0, _react.useRef)(false);
  var handleBlur = (0, _react.useCallback)(function (e) {
    if (escapeRef.current) {
      setCurrentValue(prevValue);
      setUnformattedValue(unformattedPrevValue);
      if (onChange) onChange(String(unformattedPrevValue));
    } else {
      if (e.target.value.trim().length > 0 && prevValue !== e.target.value) {
        var _value = (0, _currencyUtils.formatNumber)(currentValue, thousandSeparator, decimalSeparator, trimDecimals, decimalLength, disableAbbreviation ? "long" : "short");
        setPrevValue(_value);
        setUnformattedPrevValue((0, _currencyUtils.formatNumber)(currentValue, "", decimalSeparator, false, decimalLength, "long"));
        setUnformattedValue((0, _currencyUtils.formatNumber)(currentValue, "", decimalSeparator, false, decimalLength, "long"));
        setCurrentValue(_value);
        typeof onSave === "function" && onSave(e);
      } else {
        if (!allowEmptyValue) {
          setCurrentValue(prevValue);
          setUnformattedValue(unformattedPrevValue);
          typeof onCancel === "function" && onCancel(e);
        } else {
          if (typeof onSave === "function" && prevValue !== e.target.value) {
            onSave(e);
          } else {
            typeof onCancel === "function" && onCancel(e);
          }
        }
      }
    }
    setFocused(false);
    typeof onBlur === "function" && onBlur(e);
  }, [onBlur, prevValue, unformattedPrevValue, onChange, currentValue, thousandSeparator, decimalSeparator, trimDecimals, decimalLength, disableAbbreviation, onSave, allowEmptyValue, onCancel]);
  var updateValue = (0, _react.useCallback)(function (type) {
    var preformattedValue = String(unformattedValue).replace(thousandSeparator, "").replace(decimalSeparator, ".");
    var numericValue = parseFloat(preformattedValue);
    if (isNaN(numericValue)) return;
    if (max !== undefined && numericValue > Number(max) || min !== undefined && numericValue < Number(min)) {
      return;
    }
    var newValue = numericValue;
    if (type === "increment") {
      if (max === undefined || numericValue + step <= Number(max)) {
        newValue = numericValue + step;
      } else if (numericValue < Number(max)) {
        newValue = Number(max);
      } else {
        return;
      }
    } else if (type === "decrement") {
      if (min === undefined || numericValue - step >= Number(min)) {
        newValue = numericValue - step;
      } else if (numericValue > Number(min)) {
        newValue = Number(min);
      } else {
        return;
      }
    }
    var formattedValue;
    if (decimalLength !== undefined) {
      formattedValue = newValue.toFixed(decimalLength);
    } else {
      formattedValue = newValue.toFixed(2);
    }
    if (decimalLength !== undefined && decimalLength === 0) {
      formattedValue = Math.round(newValue).toString();
    }
    if (formattedValue.includes(".") || formattedValue.includes(",")) {
      formattedValue = formattedValue.replace(".", decimalSeparator);
    }
    setUnformattedValue(formattedValue);
    setCurrentValue(formattedValue);
    if (onChange) onChange(formattedValue);
  }, [unformattedValue, thousandSeparator, decimalSeparator, decimalLength, onChange, step, min, max]);
  var handleKeyDown = (0, _react.useCallback)(function (e) {
    if (e.key === "Enter") {
      e.target.blur();
      if (typeof onEnterKeyPress === "function") onEnterKeyPress(e.target.value);
    }
    if (e.key === "ArrowLeft") {
      return;
    }
    if (e.key === "ArrowRight") {
      return;
    }
    if (e.key === "ArrowUp") {
      e.preventDefault();
      updateValue("increment");
    }
    if (e.key === "ArrowDown") {
      e.preventDefault();
      updateValue("decrement");
    }
    if (e.key === "Escape") {
      escapeRef.current = true;
      e.target.blur();
      typeof onCancel === "function" && onCancel(e);
      escapeRef.current = false;
    }
    if (e.key === "Backspace") {
      return;
    }
    if (e.key === "Delete") {
      return;
    }
    if ((e.metaKey || e.ctrlKey) && e.key === "a") {
      var _inputRef$current;
      (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.select();
      return;
    }
    if (e.key === "Tab") {
      return;
    }
    if ((e.metaKey || e.ctrlKey) && e.key === "v") {
      e.preventDefault();
      return;
    }

    // Disallow "-" if min is 0 or greater
    if (e.key === "-" && min !== undefined && Number(min) >= 0) {
      e.preventDefault();
      return;
    }

    // Disallow decimal separator if decimalLength is 0
    if (e.key === decimalSeparator && decimalLength === 0) {
      e.preventDefault();
      return;
    }
    var input = e.target;
    var currentValue = input.value;
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var newValue = currentValue.substring(0, start) + e.key + currentValue.substring(end);
    if (!validation(newValue, Boolean(disableMacros), decimalSeparator, decimalLength !== null && decimalLength !== void 0 ? decimalLength : 0, limit)) {
      e.preventDefault();
      return;
    }
  }, [decimalLength, decimalSeparator, disableMacros, inputRef, limit, min, onCancel, onEnterKeyPress, updateValue, validation]);
  var handleChange = (0, _react.useCallback)(function (e) {
    var inputValue = e.target.value;
    var numericInput = disableMacros ? inputValue : inputValue.replace(/(\d+(?:[.,]\d+)?)([kmbtKMBT])/, function (_, num, unit) {
      var normalizedNum = num.replace(",", ".");
      var parts = normalizedNum.split(".");
      var integerPart = parts[0];
      var fractionalPart = parts[1];
      var newInteger = parseInt(integerPart, 10) * _currencyUtils.currencyMultiplier[unit.toLowerCase()];
      var result = newInteger.toString();
      if (fractionalPart !== undefined) {
        result += decimalSeparator + fractionalPart;
      }
      return result;
    });
    setCurrentValue(numericInput);
    setUnformattedValue(numericInput);
    if (onChange) onChange(numericInput);
  }, [disableMacros, decimalSeparator, onChange]);
  var handleClick = (0, _react.useCallback)(function (e) {
    if (typeof onClick === "function") {
      onClick(e);
    }
  }, [onClick]);
  var handleFocus = (0, _react.useCallback)(function () {
    setCurrentValue(unformattedValue);
    setFocused(true);
  }, [unformattedValue]);
  var handleDoubleClick = (0, _react.useCallback)(function () {
    if (inputRef.current) {
      var _inputRef$current2;
      (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || _inputRef$current2.select();
    }
  }, [inputRef]);
  return {
    value: currentValue,
    onBlur: handleBlur,
    onKeyDown: handleKeyDown,
    onChange: handleChange,
    onClick: handleClick,
    onDoubleClick: handleDoubleClick,
    onFocus: handleFocus,
    focused,
    unformattedValue
  };
};
//# sourceMappingURL=useInputNumber.js.map