"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.CurrencyHelper = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _utils = require("../../lib/utils");

var _client = require("../../lib/client");



var _constants = require("./constants");var _CurrencyHelper;var























CurrencyHelper = exports.CurrencyHelper = /*#__PURE__*/function () {function CurrencyHelper() {}CurrencyHelper.







  getOptions = function getOptions(options) {
    return (0, _extends2.default)({}, CurrencyHelper.defaultOptions, options);
  };CurrencyHelper.

  getInfo = function getInfo(value, options) {
    var raw = CurrencyHelper.unformatString(value);
    var formatted = CurrencyHelper.formatString(value, options);
    var cursorMap = CurrencyHelper.getCursorMap(formatted, options);

    return { raw: raw, formatted: formatted, cursorMap: cursorMap };
  };CurrencyHelper.

  getCursorMap = function getCursorMap(formatted, formattingOptions) {
    var options = CurrencyHelper.getOptions(formattingOptions);
    var regexp = new RegExp(options.thousandsDelimiter);
    var cursorMap = [];

    var index = formatted.length;
    var cursor = formatted.length;
    var skip = 0;

    while (index >= 0) {
      cursorMap[index] = cursor;

      var ignoredSymbol = regexp.exec(formatted[index - 1]);
      if (ignoredSymbol) {
        ++skip;
      } else {
        cursor = cursor - skip - 1;
        skip = 0;
      }
      --index;
    }
    return cursorMap;
  };CurrencyHelper.

  format = function format(value, options) {
    if ((0, _utils.isNullable)(value)) {
      return '';
    }
    return CurrencyHelper.formatString(this.toDecimalString(value), options);
  };CurrencyHelper.

  parse = function parse(value) {
    var cleaned = CurrencyHelper.unformatString(value);
    if (!cleaned) {
      return null;
    }

    var destructed = CurrencyHelper.destructString(cleaned);
    if (!destructed) {
      return null;
    }

    var result =
    destructed.sign + (destructed.integer || '0') + (destructed.delimiter || '.') + (destructed.fraction || '0');

    return parseFloat(result);
  };CurrencyHelper.

  unformatString = function unformatString(value) {
    return value.
    replace(/\s/g, '').
    replace(',', '.').
    replace(/[\u2212\u002D\uFE63\uFF0D\u2012\u2013\u2014\u2015]/g, '-');
  };CurrencyHelper.

  formatForClipboard = function formatForClipboard(value) {
    return CurrencyHelper.unformatString(value).replace('.', ',');
  };CurrencyHelper.

  formatString = function formatString(value, formattingOptions) {
    var unformattedValue = CurrencyHelper.unformatString(value);
    var destructed = CurrencyHelper.destructString(unformattedValue) || {
      sign: '',
      integer: '',
      delimiter: '',
      fraction: ''
    };

    var sign = destructed.sign,integer = destructed.integer,delimiter = destructed.delimiter;
    var fraction = destructed.fraction;
    var fractionDigits = fraction.length;

    var options = CurrencyHelper.getOptions(formattingOptions);
    if (options.hideTrailingZeros) {
      fraction = fraction.replace(/0+$/, '');
      fractionDigits = fraction.length;
    } else if (options.fractionDigits !== null) {
      fractionDigits = options.fractionDigits;
    }

    var parts = [];

    var blockSize = 3;
    var start = (integer.length - 1) % blockSize - blockSize + 1;
    for (var i = start; i < integer.length; i += blockSize) {
      parts.push(integer.substring(Math.max(i, 0), i + blockSize));
    }

    var result = parts.join(options.thousandsDelimiter);

    if (delimiter || fractionDigits) {
      result += ',';
      result += fraction.padEnd(Math.min(fractionDigits, _constants.MAX_SAFE_DIGITS), '0');
    }

    if (sign) {
      result = options.minusSign + result;
    }

    return result;
  };CurrencyHelper.

  isValidString = function isValidString(value, options) {
    var unformattedValue = CurrencyHelper.unformatString(value);
    var destructed = CurrencyHelper.destructString(unformattedValue);

    if (!destructed) {
      return false;
    }

    var sign = destructed.sign,integer = destructed.integer,delimiter = destructed.delimiter,fraction = destructed.fraction;

    if (options.unsigned && sign) {
      return false;
    }

    if (options.fractionDigits === 0 && delimiter) {
      return false;
    }

    var integerDigits = integer === '0' ? 0 : integer.length;
    var fractionDigits = fraction.length;

    if ((0, _utils.isNonNullable)(options.integerDigits) && integerDigits > options.integerDigits) {
      return false;
    }

    if ((0, _utils.isNullable)(options.integerDigits) && integerDigits > _constants.MAX_SAFE_DIGITS - (options.fractionDigits || 0)) {
      return false;
    }

    if ((0, _utils.isNonNullable)(options.fractionDigits) && fractionDigits > options.fractionDigits) {
      return false;
    }

    return integerDigits + fractionDigits <= _constants.MAX_SAFE_DIGITS;
  };CurrencyHelper.

  extractValid = function extractValid(value, options) {
    var unformattedValue = CurrencyHelper.unformatString(value);

    var match = /[-.\d]+/.exec(unformattedValue);

    if (!match) {
      return '';
    }

    var token = match[0].substr(0, _constants.MAX_ALLOWED_CHARS);

    for (var i = token.length; i >= 0; --i) {
      var result = token.substr(0, i);
      if (CurrencyHelper.isValidString(result, options)) {
        return result;
      }
    }

    return '';
  };CurrencyHelper.

  destructString = function destructString(value) {
    var match = /^(-)?(\d*)?(\.)?(\d*)?(e)?([-+]?\d+)?$/.exec(value);
    if (!match) {
      return null;
    }
    var _match$ = match[1],sign = _match$ === void 0 ? '' : _match$,_match$2 = match[2],integer = _match$2 === void 0 ? '' : _match$2,_match$3 = match[3],delimiter = _match$3 === void 0 ? '' : _match$3,_match$4 = match[4],fraction = _match$4 === void 0 ? '' : _match$4,_match$5 = match[6],exponent = _match$5 === void 0 ? '' : _match$5;
    return { sign: sign, integer: integer, delimiter: delimiter, fraction: fraction, exponent: exponent };
  };return CurrencyHelper;}();_CurrencyHelper = CurrencyHelper;CurrencyHelper.defaultOptions = { fractionDigits: null, hideTrailingZeros: false, thousandsDelimiter: _client.isSafari ? _constants.SPACE : _constants.THIN_SPACE, minusSign: _constants.MINUS_SIGN };CurrencyHelper.

toDecimalString = function (number) {
  if (!number.toString().includes('e')) {
    return number.toString();
  }
  var destructed = _CurrencyHelper.destructString(number.toExponential());
  if (destructed === null) {
    return '';
  }
  var _destructed$sign = destructed.sign,sign = _destructed$sign === void 0 ? '' : _destructed$sign,integer = destructed.integer,fraction = destructed.fraction,exponent = destructed.exponent;
  var intExponent = parseInt(exponent || '0');
  if (intExponent > 0) {
    return [sign, integer, fraction, '0'.repeat(intExponent - fraction.length)].join('');
  }
  return [sign, '0.', ' 0'.repeat(-intExponent - 1), integer, fraction].join('');
};