import { getStateName } from "../index.js";

/**
 * Utility to format names of Senate contests
 *
 * @param {Object} metadata
 * @param {Object} opts
 * @param {string} opts.format
 * @returns {string}
 */
const senContestName = function senContestName(metadata, opts = {}) {
  const {
    stateFips,
    isSpecial
  } = metadata;
  const {
    format = 'long'
  } = opts;

  // e.g., "NM"
  if (format === 'tiny') {
    return getStateName(stateFips, {
      format: 'abbr'
    });
  }

  // e.g., "NM Senate", "NM Sen. Special"
  if (format === 'short') {
    const stateName = getStateName(stateFips, {
      format: 'abbr'
    });
    return isSpecial ? `${stateName} Sen. Special` : `${stateName} Senate`;
  }

  // e.g., "New Mexico Senate", "New Mexico Senate (Special)"
  if (format === 'medium') {
    const special = isSpecial ? ' (Special)' : '';
    const stateName = getStateName(stateFips, {
      format: 'name'
    });
    return `${stateName} Senate${special}`;
  }

  // e.g., "Senate", "Senate (Special)"
  if (format === 'no-state') {
    const special = isSpecial ? ' (Special)' : '';
    return `Senate${special}`;
  }

  // e.g., "U.S. Senator, New Mexico", "U.S. Senator, New Mexico (Special
  // election)"
  const specialSuffix = isSpecial ? ' (Special election)' : '';
  const stateName = getStateName(stateFips, {
    format: 'name'
  });
  return `U.S. Senator, ${stateName}${specialSuffix}`;
};
export default senContestName;