import { useState, useEffect } from 'react';

// via https://dev.to/gabe_ragland/debouncing-with-react-hooks-jci
function useDebounce(value, delay) {
  // State and setters for debounced value
  const [debouncedValue, setDebouncedValue] = useState(value);
  useEffect(() => {
    // Set debouncedValue to value (passed in) after the specified delay
    const handler = setTimeout(() => {
      setDebouncedValue(value);
    }, delay);

    // Return a cleanup function that will be called every time
    // useEffect is re-called. useEffect will only be re-called
    // if value changes (see the inputs array below).
    // This is how we prevent debouncedValue from changing if value is
    // changed within the delay period. Timeout gets cleared and restarted.
    // To put it in context, if the user is typing within our app's
    // search box, we don't want the debouncedValue to update until
    // they've stopped typing for more than 500ms.
    return () => {
      clearTimeout(handler);
    };
  },
  // Only re-call effect if value changes
  // You could also add the "delay" var to inputs array if you
  // need to be able to change that dynamically.
  [value]);
  return debouncedValue;
}

// TODO: This is a dup of https://github.com/transferwise/neptune-web/blob/main/packages/components/src/moneyInput/currencyFormatting.js
// It's not exported out of neptune-web correctly and cannot get it working with Jest.
const currencyDecimals = {
    BIF: 0,
    BYR: 0,
    CLP: 0,
    DJF: 0,
    GNF: 0,
    JPY: 0,
    KMF: 0,
    KRW: 0,
    MGA: 0,
    PYG: 0,
    RWF: 0,
    VND: 0,
    VUV: 0,
    XAF: 0,
    XOF: 0,
    XPF: 0,
    // technically HUF does have decimals, but due to the exchange rate banks
    // do not accept decimal amounts
    HUF: 0,
    BHD: 3,
    JOD: 3,
    KWD: 3,
    OMR: 3,
    TND: 3,
};
const DEFAULT_LOCALE = 'en-GB';
const DEFAULT_CURRENCY_DECIMALS = 2;
function isNumberLocaleSupported() {
    const number = 1234;
    const numberString = number.toLocaleString && number.toLocaleString(DEFAULT_LOCALE);
    return numberString === '1,234';
}
function getValidLocale(locale) {
    if (!locale) {
        return DEFAULT_LOCALE;
    }
    try {
        const noUnderscoreLocale = locale.replace(/_/, '-');
        Intl.NumberFormat(noUnderscoreLocale);
        return noUnderscoreLocale;
    }
    catch (_a) {
        return DEFAULT_LOCALE;
    }
}
const getCurrencyDecimals = (currency = '') => {
    const upperCaseCurrency = currency.toUpperCase();
    if (Object.prototype.hasOwnProperty.call(currencyDecimals, upperCaseCurrency)) {
        return currencyDecimals[upperCaseCurrency];
    }
    return DEFAULT_CURRENCY_DECIMALS;
};
const getDecimalSeparator = (locale) => {
    return isNumberLocaleSupported() ? (1.1).toLocaleString(locale)[1] : '.';
};
function parseAmount(number, currency, locale) {
    const validLocale = getValidLocale(locale);
    const precision = getCurrencyDecimals(currency);
    const groupSeparator = isNumberLocaleSupported() ? (10000).toLocaleString(validLocale)[2] : ',';
    const decimalSeparator = getDecimalSeparator(validLocale);
    const numberWithStandardDecimalSeparator = (number ? `${number}` : '')
        .replace(new RegExp(`\\${groupSeparator}`, 'g'), '')
        .replace(new RegExp(`\\${decimalSeparator}`, 'g'), '.')
        .replace(/[^0-9.]/g, '');
    const parsedAmount = parseFloat(parseFloat(numberWithStandardDecimalSeparator).toFixed(precision));
    return Math.abs(parsedAmount);
}

export { parseAmount as p, useDebounce as u };
//# sourceMappingURL=parseAmount-c0794650.js.map
