import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _extends from "@babel/runtime/helpers/esm/extends";
/* eslint-disable @typescript-eslint/no-use-before-define */

import { escapeRegExp } from './escapeRegExp';
import { getSuffix } from './getSuffix';
/**
 * Format value with decimal separator, group separator and prefix
 */
export var formatValue = function formatValue(options) {
  var _value = options.value,
    decimalSeparator = options.decimalSeparator,
    intlConfig = options.intlConfig,
    decimalScale = options.decimalScale,
    _options$prefix = options.prefix,
    prefix = _options$prefix === void 0 ? '' : _options$prefix,
    _options$suffix = options.suffix,
    suffix = _options$suffix === void 0 ? '' : _options$suffix;
  if (_value === '' || _value === undefined) {
    return '';
  }
  if (_value === '-') {
    return '-';
  }
  var isNegative = new RegExp("^\\d?-".concat(prefix ? "".concat(escapeRegExp(prefix), "?") : '', "\\d")).test(_value);
  var value = decimalSeparator !== '.' ? replaceDecimalSeparator(_value, decimalSeparator, isNegative) : _value;
  if (decimalSeparator && decimalSeparator !== '-' && value.startsWith(decimalSeparator)) {
    value = '0' + value;
  }
  var defaultNumberFormatOptions = {
    minimumFractionDigits: decimalScale || 0,
    maximumFractionDigits: 20
  };
  var numberFormatter = intlConfig ? new Intl.NumberFormat(intlConfig.locale, intlConfig.currency ? _extends({}, defaultNumberFormatOptions, {
    style: 'currency',
    currency: intlConfig.currency
  }) : defaultNumberFormatOptions) : new Intl.NumberFormat(undefined, defaultNumberFormatOptions);
  var parts = numberFormatter.formatToParts(Number(value));
  var formatted = replaceParts(parts, options);

  // Does intl formatting add a suffix?
  var intlSuffix = getSuffix(formatted, _extends({}, options));

  // Include decimal separator if user input ends with decimal separator
  var includeDecimalSeparator = _value.slice(-1) === decimalSeparator ? decimalSeparator : '';
  var _ref = value.match(RegExp('\\d+\\.(\\d+)')) || [],
    _ref2 = _slicedToArray(_ref, 2),
    decimals = _ref2[1];

  // Keep original decimal padding if no decimalScale
  if (decimalScale === undefined && decimals && decimalSeparator) {
    if (formatted.includes(decimalSeparator)) {
      formatted = formatted.replace(RegExp("(\\d+)(".concat(escapeRegExp(decimalSeparator), ")(\\d+)"), 'g'), "$1$2".concat(decimals));
    } else {
      if (intlSuffix && !suffix) {
        formatted = formatted.replace(intlSuffix, "".concat(decimalSeparator).concat(decimals).concat(intlSuffix));
      } else {
        formatted = "".concat(formatted).concat(decimalSeparator).concat(decimals);
      }
    }
  }
  if (suffix && includeDecimalSeparator) {
    return "".concat(formatted).concat(includeDecimalSeparator).concat(suffix);
  }
  if (intlSuffix && includeDecimalSeparator) {
    return formatted.replace(intlSuffix, "".concat(includeDecimalSeparator).concat(intlSuffix));
  }
  if (intlSuffix && suffix) {
    return formatted.replace(intlSuffix, "".concat(includeDecimalSeparator).concat(suffix));
  }
  return [formatted, includeDecimalSeparator, suffix].join('');
};

/**
 * Before converting to Number, decimal separator has to be .
 */
var replaceDecimalSeparator = function replaceDecimalSeparator(value, decimalSeparator, isNegative) {
  var newValue = value;
  if (decimalSeparator && decimalSeparator !== '.') {
    newValue = newValue.replace(RegExp(escapeRegExp(decimalSeparator), 'g'), '.');
    if (isNegative && decimalSeparator === '-') {
      newValue = "-".concat(newValue.slice(1));
    }
  }
  return newValue;
};
var replaceParts = function replaceParts(parts, _ref3) {
  var prefix = _ref3.prefix,
    groupSeparator = _ref3.groupSeparator,
    decimalSeparator = _ref3.decimalSeparator,
    decimalScale = _ref3.decimalScale,
    _ref3$disableGroupSep = _ref3.disableGroupSeparators,
    disableGroupSeparators = _ref3$disableGroupSep === void 0 ? false : _ref3$disableGroupSep;
  return parts.reduce(function (prev, _ref4, i) {
    var type = _ref4.type,
      value = _ref4.value;
    if (i === 0 && prefix) {
      if (type === 'minusSign') {
        return [value, prefix];
      }
      if (type === 'currency') {
        return [].concat(_toConsumableArray(prev), [prefix]);
      }
      return [prefix, value];
    }
    if (type === 'currency') {
      return prefix ? prev : [].concat(_toConsumableArray(prev), [value]);
    }
    if (type === 'group') {
      return !disableGroupSeparators ? [].concat(_toConsumableArray(prev), [groupSeparator !== undefined ? groupSeparator : value]) : prev;
    }
    if (type === 'decimal') {
      if (decimalScale !== undefined && decimalScale === 0) {
        return prev;
      }
      return [].concat(_toConsumableArray(prev), [decimalSeparator !== undefined ? decimalSeparator : value]);
    }
    if (type === 'fraction') {
      return [].concat(_toConsumableArray(prev), [decimalScale !== undefined ? value.slice(0, decimalScale) : value]);
    }
    return [].concat(_toConsumableArray(prev), [value]);
  }, ['']).join('');
};