'use strict';

var formatting = require('@transferwise/formatting');

const getDecimalSeparator = (currency, locale) => {
  return formatting.formatAmount(1.1, currency, locale).replace(/\p{Number}/gu, '');
};
const getGroupSeparator = (currency, locale) => {
  return formatting.formatAmount(10000000, currency, locale).replace(/\p{Number}/gu, '')[0];
};
const getDecimalCount = (currency, locale) => {
  const decimalSeparator = getDecimalSeparator(currency, locale);
  if (!decimalSeparator) {
    return 0;
  }
  const parts = formatting.formatAmount(1.1, currency, locale).split(decimalSeparator);
  return parts.length === 2 ? parts[1].length : 0;
};
const getEnteredDecimalsCount = (value, decimalSeparator) => {
  return value.split(decimalSeparator)[1]?.length ?? 0;
};
const getUnformattedNumber = ({
  value: formattedValue,
  currency,
  locale
}) => {
  const groupSeparator = getGroupSeparator(currency, locale);
  const decimalSeparator = getDecimalSeparator(currency, locale);
  if (!formattedValue) {
    return null;
  }
  // parseFloat can't handle thousands separators
  const withoutGroupSeparator = groupSeparator ? formattedValue.replace(/ /gu, '').replace(new RegExp(`\\${groupSeparator}`, 'g'), '') : formattedValue;
  // parseFloat can only handle . as decimal separator
  const withNormalisedDecimalSeparator = decimalSeparator ? withoutGroupSeparator.replace(decimalSeparator, '.') : withoutGroupSeparator;
  const parsedValue = Number.parseFloat(withNormalisedDecimalSeparator);
  return parsedValue;
};
const getFormattedString = ({
  value: unformattedValue,
  currency,
  locale,
  alwaysShowDecimals = false
}) => {
  const decimalSeparator = getDecimalSeparator(currency, locale);
  // formatAmount rounds extra decimals, so 1.999 will become 2. Instead we will manually strip extra decimals so that it becomes 1.99 after formatting
  const decimalCount = getDecimalCount(currency, locale);
  const unformattedString = unformattedValue.toString();
  const [integerPart, decimalPart] = decimalSeparator ? unformattedString.split(decimalSeparator) : [unformattedString, undefined];
  const formattedDecimalPart = decimalPart ? decimalPart.slice(0, decimalCount) : '';
  const sanitisedUnformattedValue = Number.parseFloat(`${integerPart}.${formattedDecimalPart}`);
  return formatting.formatAmount(sanitisedUnformattedValue, currency, locale, {
    alwaysShowDecimals
  });
};
const isInputPossiblyOverflowing = ({
  ref,
  value
}) => {
  const textLength = value.length;
  const inputWidth = ref.current?.clientWidth;
  if (!inputWidth || !textLength) {
    return;
  }
  const maxCharactersWithoutOverflow = Math.floor(inputWidth / 19);
  return textLength > maxCharactersWithoutOverflow;
};
const allowedInputKeys = new Set(['Backspace', 'Delete', ',', '.', 'ArrowLeft', 'ArrowRight', 'Enter', 'Tab']);
const isAllowedInputKey = e => {
  const {
    metaKey,
    key,
    ctrlKey
  } = e;
  const isNumberKey = !Number.isNaN(Number.parseInt(key, 10));
  return isNumberKey || metaKey || ctrlKey || allowedInputKeys.has(key);
};

exports.getDecimalCount = getDecimalCount;
exports.getDecimalSeparator = getDecimalSeparator;
exports.getEnteredDecimalsCount = getEnteredDecimalsCount;
exports.getFormattedString = getFormattedString;
exports.getGroupSeparator = getGroupSeparator;
exports.getUnformattedNumber = getUnformattedNumber;
exports.isAllowedInputKey = isAllowedInputKey;
exports.isInputPossiblyOverflowing = isInputPossiblyOverflowing;
//# sourceMappingURL=utils.js.map
