import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { parseAbbrValue } from './parseAbbrValue';
import { removeSeparators } from './removeSeparators';
import { removeInvalidChars } from './removeInvalidChars';
import { escapeRegExp } from './escapeRegExp';
/**
 * Remove prefix, separators and extra decimals from value
 */
export var cleanValue = function cleanValue(_ref) {
  var value = _ref.value,
    _ref$groupSeparator = _ref.groupSeparator,
    groupSeparator = _ref$groupSeparator === void 0 ? ',' : _ref$groupSeparator,
    _ref$decimalSeparator = _ref.decimalSeparator,
    decimalSeparator = _ref$decimalSeparator === void 0 ? '.' : _ref$decimalSeparator,
    _ref$allowDecimals = _ref.allowDecimals,
    allowDecimals = _ref$allowDecimals === void 0 ? true : _ref$allowDecimals,
    _ref$decimalsLimit = _ref.decimalsLimit,
    decimalsLimit = _ref$decimalsLimit === void 0 ? 2 : _ref$decimalsLimit,
    _ref$allowNegativeVal = _ref.allowNegativeValue,
    allowNegativeValue = _ref$allowNegativeVal === void 0 ? true : _ref$allowNegativeVal,
    _ref$disableAbbreviat = _ref.disableAbbreviations,
    disableAbbreviations = _ref$disableAbbreviat === void 0 ? false : _ref$disableAbbreviat,
    _ref$prefix = _ref.prefix,
    prefix = _ref$prefix === void 0 ? '' : _ref$prefix,
    _ref$transformRawValu = _ref.transformRawValue,
    transformRawValue = _ref$transformRawValu === void 0 ? function (rawValue) {
      return rawValue;
    } : _ref$transformRawValu;
  var transformedValue = transformRawValue(value);
  if (transformedValue === '-') {
    return transformedValue;
  }
  var abbreviations = disableAbbreviations ? [] : ['k', 'm', 'b'];
  var reg = new RegExp("((^|\\D)-\\d)|(-".concat(escapeRegExp(prefix), ")"));
  var isNegative = reg.test(transformedValue);

  // Is there a digit before the prefix? eg. 1$
  var _ref2 = RegExp("(\\d+)-?".concat(escapeRegExp(prefix))).exec(value) || [],
    _ref3 = _slicedToArray(_ref2, 2),
    prefixWithValue = _ref3[0],
    preValue = _ref3[1];
  var withoutPrefix = prefix ? prefixWithValue ? transformedValue.replace(prefixWithValue, '').concat(preValue) : transformedValue.replace(prefix, '') : transformedValue;
  var withoutSeparators = removeSeparators(withoutPrefix, groupSeparator);
  var withoutInvalidChars = removeInvalidChars(withoutSeparators, [groupSeparator, decimalSeparator].concat(abbreviations));
  var valueOnly = withoutInvalidChars;
  if (!disableAbbreviations) {
    // disallow letter without number
    if (abbreviations.some(function (letter) {
      return letter === withoutInvalidChars.toLowerCase().replace(decimalSeparator, '');
    })) {
      return '';
    }
    var parsed = parseAbbrValue(withoutInvalidChars, decimalSeparator);
    if (parsed) {
      valueOnly = String(parsed);
    }
  }
  var includeNegative = isNegative && allowNegativeValue ? '-' : '';
  if (decimalSeparator && valueOnly.includes(decimalSeparator)) {
    var _withoutInvalidChars$ = withoutInvalidChars.split(decimalSeparator),
      _withoutInvalidChars$2 = _slicedToArray(_withoutInvalidChars$, 2),
      _int = _withoutInvalidChars$2[0],
      decimals = _withoutInvalidChars$2[1];
    var trimmedDecimals = decimalsLimit && decimals ? decimals.slice(0, decimalsLimit) : decimals;
    var includeDecimals = allowDecimals ? "".concat(decimalSeparator).concat(trimmedDecimals) : '';
    return "".concat(includeNegative).concat(_int).concat(includeDecimals);
  }
  return "".concat(includeNegative).concat(valueOnly);
};