import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { escapeRegExp } from './escapeRegExp';

/**
 * Abbreviate number eg. 1000 = 1k
 *
 * Source: https://stackoverflow.com/a/9345181
 */
export var abbrValue = function abbrValue(value) {
  var decimalSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';
  var _decimalPlaces = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
  if (value > 999) {
    var valueLength = ('' + value).length;
    var p = Math.pow;
    var d = p(10, _decimalPlaces);
    valueLength -= valueLength % 3;
    var _abbrValue = Math.round(value * d / p(10, valueLength)) / d + ' kMGTPE'[valueLength / 3];
    return _abbrValue.replace('.', decimalSeparator);
  }
  return String(value);
};
var abbrMap = {
  k: 1000,
  m: 1000000,
  b: 1000000000
};

/**
 * Parse a value with abbreviation e.g 1k = 1000
 */
export var parseAbbrValue = function parseAbbrValue(value) {
  var decimalSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';
  var reg = new RegExp("(\\d+(".concat(escapeRegExp(decimalSeparator), "\\d*)?)([kmb])$"), 'i');
  var match = value.match(reg);
  if (match) {
    var _match = _slicedToArray(match, 4),
      digits = _match[1],
      abbr = _match[3];
    var multiplier = abbrMap[abbr.toLowerCase()];
    return Number(digits.replace(decimalSeparator, '.')) * multiplier;
  }
  return undefined;
};