import formatNum from './formatNumber';
import formatPercent from './formatPercent';
import formatSentence from './formatSentenceNumber';
import formatAbbreviation from './formatAbbreviation';

/**
 * Format a number for presentation. This could be a big integer that needs
 * comma separation, a percentage represented as a number between 0 and 1, etc.
 *
 * The options configuation accepts all the same options as [toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#parameters)
 * Note that the default `maximumFractionDigits` value is 1, so if you need to set a minimum higher than one
 * you'll also need to override the maximumFractionDigits.
 *
 * @param {number} n
 *   The number to format
 * @param {Object} [options]
 *   Additional configuration options:
 *   - `format`: The format to use. Type: `'number'|'percent'|'sentence'`.
 *     Default: `'number'`.
 *   - `locale`: The locale to use for number formatting. Type: `string`.
 *     Default: `'en-US'`.
 * @returns {string}
 */
const formatNumber = function formatNumber(n, options = {}) {
  const {
    format = 'number',
    locale = 'en-US',
    ...toLocaleStringOpts
  } = options;
  const formatOpts = {
    locale,
    ...toLocaleStringOpts
  };
  if (format === 'percent') {
    return formatPercent(n, formatOpts);
  }
  if (format === 'sentence') {
    return formatSentence(n, formatOpts);
  }
  if (format === 'abbreviation') {
    return formatAbbreviation(n, formatOpts);
  }
  return formatNum(n, formatOpts);
};
export default formatNumber;