"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.parseInputValue = exports.handleThousandSeparatorRemoval = exports.getThousandsSeparator = exports.getMinusSign = exports.getDecimalSign = exports.formatNumber = exports.calculateCaretPosition = void 0;
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 escapeStringRegexp = function escapeStringRegexp(s) {
  return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
};
var getDecimalSign = exports.getDecimalSign = function getDecimalSign(intl) {
  return intl.formatNumberToParts(0.1)[1].value;
};
var getThousandsSeparator = exports.getThousandsSeparator = function getThousandsSeparator(intl) {
  return intl.formatNumberToParts(1000)[1].value;
};
var getMinusSign = exports.getMinusSign = function getMinusSign(intl) {
  return intl.formatNumberToParts(-1)[0].value;
};
var formatNumber = exports.formatNumber = function formatNumber(value, intl) {
  var minimumFractionDigits = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  return intl.formatNumber(value, {
    minimumFractionDigits: minimumFractionDigits,
    maximumFractionDigits: 20
  });
};
var getSanitizedValue = function getSanitizedValue(inputValue, replaceValue, intl) {
  var decimalSign = getDecimalSign(intl);
  var minusSign = getMinusSign(intl);
  var acceptDotAsDecimalSign = getThousandsSeparator(intl) !== '.' ? '.' : '';
  var nonNumericRegExp = new RegExp("[^".concat(minusSign).concat(decimalSign, "\\d-").concat(acceptDotAsDecimalSign, "]"), 'g');
  var replaceDecimalRegExp = new RegExp(escapeStringRegexp(decimalSign), 'g');
  var replaceMinusSignRegExp = new RegExp(escapeStringRegexp(minusSign), 'g');
  var numericValue = inputValue.replace(nonNumericRegExp, replaceValue);
  var delocalizedValue = numericValue.replace(replaceDecimalRegExp, '.').replace(replaceMinusSignRegExp, '-');
  var isNegative = delocalizedValue.startsWith('-');
  var withoutMinus = delocalizedValue.replace(/^-/, '').replace(/-/g, replaceValue);
  var sanitizedValue = withoutMinus.replace(/^\./, '0.');
  return isNegative ? "-".concat(sanitizedValue) : sanitizedValue;
};
var formatWithLeadingZeros = function formatWithLeadingZeros(integerPart, intl) {
  var isNegative = integerPart.startsWith('-');
  var positiveIntegerPart = integerPart.replace('-', '');
  var oneAdded = positiveIntegerPart.replace(/^0/, '10');
  var oneAddedParsed = Number(oneAdded);
  var oneAddedFormatted = formatNumber(oneAddedParsed, intl);
  var minusSign = getMinusSign(intl);
  var formattedWithLeadingZeros = oneAddedFormatted.replace(/^1/, '');
  return isNegative ? minusSign + formattedWithLeadingZeros : formattedWithLeadingZeros;
};
var parseInputValue = exports.parseInputValue = function parseInputValue(previousValue, previousFormattedValue, inputValue, intl) {
  var decimalSign = getDecimalSign(intl);
  var minusSign = getMinusSign(intl);
  var sanitizedValue = getSanitizedValue(inputValue, '', intl);
  var hasMultipleDecimals = sanitizedValue.split('.').length > 2;
  if (hasMultipleDecimals) {
    return {
      value: previousValue,
      formattedValue: previousFormattedValue
    };
  }
  if (sanitizedValue === '') {
    return {
      value: null,
      formattedValue: ''
    };
  }
  if (sanitizedValue === '-') {
    return {
      value: null,
      formattedValue: minusSign
    };
  }
  var hasLeadingZeros = /^((-0)|0)/g.test(sanitizedValue);
  var _sanitizedValue$split = sanitizedValue.split('.'),
    _sanitizedValue$split2 = _slicedToArray(_sanitizedValue$split, 2),
    integerPart = _sanitizedValue$split2[0],
    decimalPart = _sanitizedValue$split2[1];
  var integerPartParsed = Number(integerPart);
  var formattedIntegerPart = hasLeadingZeros ? formatWithLeadingZeros(integerPart, intl) : formatNumber(integerPartParsed, intl);
  if (sanitizedValue.endsWith('.')) {
    var formattedWithDecimal = formattedIntegerPart + decimalSign;
    return {
      value: integerPartParsed,
      formattedValue: formattedWithDecimal
    };
  }
  return {
    value: Number(sanitizedValue),
    formattedValue: formattedIntegerPart + (decimalPart ? "".concat(decimalSign).concat(decimalPart) : '')
  };
};
var findNthDigitIndex = function findNthDigitIndex(str, num, regExp) {
  var digitCount = 0;
  var i = 0;
  for (; i < str.length && digitCount !== num; i += 1) {
    if (regExp.test(str[i])) {
      digitCount += 1;
    }
  }
  return i - 1;
};
var calculateCaretPosition = exports.calculateCaretPosition = function calculateCaretPosition(inputValue, previousFormattedValue, caretPosition, intl) {
  if (caretPosition === null) return inputValue.formattedValue.length;
  var sanitizedValue = getSanitizedValue(inputValue.value, '_', intl);
  var hasMultipleDecimals = sanitizedValue.split('.').length > 2;
  if (hasMultipleDecimals) {
    var decimalSign = getDecimalSign(intl);
    return previousFormattedValue.indexOf(decimalSign) + 1;
  }
  var beforeCaret = sanitizedValue.substring(0, caretPosition);
  var allowedSymbolsRegExp = new RegExp("[^-.\\d]", 'g');
  var numberOfAllowedSymbolsBeforeCaret = beforeCaret.replace(allowedSymbolsRegExp, '').length;
  var isAllowedSymbolRegExp = new RegExp("-|\\.|\\d");
  var indexOfLastDigit = findNthDigitIndex(getSanitizedValue(inputValue.formattedValue, '_', intl), numberOfAllowedSymbolsBeforeCaret, isAllowedSymbolRegExp);
  return indexOfLastDigit + 1;
};
var handleThousandSeparatorRemoval = exports.handleThousandSeparatorRemoval = function handleThousandSeparatorRemoval(e, intl) {
  var el = e.target;
  var key = e.key;
  var selectionStart = el.selectionStart,
    selectionEnd = el.selectionEnd,
    _el$value = el.value,
    value = _el$value === void 0 ? '' : _el$value;
  var thousandSeparator = getThousandsSeparator(intl);
  if (selectionStart === null || selectionStart !== selectionEnd) {
    return;
  }
  var shiftLeft = selectionStart - 1;
  if (key === 'Backspace' && value[selectionStart - 1] === thousandSeparator) {
    el.setSelectionRange(shiftLeft, shiftLeft);
  }
  var shiftRight = selectionStart + 1;
  if (key === 'Delete' && value[selectionStart] === thousandSeparator) {
    el.setSelectionRange(shiftRight, shiftRight);
  }
};