const ABBREVIATIONS = {
  bi: {
    word: 'billion',
    letter: 'b'
  },
  mi: {
    word: 'million',
    letter: 'm'
  }
};

/**
 * Format a number with it's place as a letter
 *
 *
 * @param {number} n
 * @returns {string}
 */
const formatAbbreviation = function formatAbbreviation(n, options = {}) {
  const {
    locale = 'en-US',
    roundTo = 0,
    abbreviation: abbreviationType = 'word',
    ...toLocaleStringOpts
  } = options;
  if (typeof n !== 'number') return undefined;
  let displayNum = n;
  let round = false;
  let abbr = '';
  if (n >= 1000000000) {
    displayNum = n / 1000000000;
    abbr = 'bi';
    round = true;
  } else if (n >= 1000000) {
    displayNum = n / 1000000;
    abbr = 'mi';
    round = true;
  }
  if (round) {
    displayNum = displayNum.toFixed(roundTo);
  }
  const abbreviationConfig = ABBREVIATIONS[abbr];
  const abbrString = abbreviationConfig ? abbreviationConfig[abbreviationType] : '';
  const spacer = abbreviationType === 'word' ? ' ' : '';
  return `${displayNum.toLocaleString(locale, toLocaleStringOpts)}${spacer}${abbrString}`;
};
export default formatAbbreviation;