import { ordinal } from 'journalize';
import { getStateName } from "../index.js";

/**
 * Utility to format names of U.S. House contests
 *
 * @param {Object} metadata
 * @param {Object} opts
 * @param {string} opts.format
 * @returns {string}
 */
const cdContestName = function cdContestName(metadata, opts = {}) {
  const {
    stateFips,
    districtNumber
  } = metadata;
  const {
    format = 'long'
  } = opts;
  const isAtLarge = districtNumber === '00';

  // e.g., "1st", "00"
  if (format === 'tiny') {
    return isAtLarge ? districtNumber : ordinal(parseInt(districtNumber, 10));
  }

  // e.g., "NM-01", "AK-AL"
  // NOTE: same formatting for both short and medium
  if (format === 'short' || format === 'medium') {
    const district = isAtLarge ? 'AL' : districtNumber;
    const stateName = getStateName(stateFips, {
      format: 'abbr'
    });
    return `${stateName}-${district}`;
  }

  // e.g., "New Mexico's 1st district", "Alaska's At-large district"
  const stateName = getStateName(stateFips, {
    format: 'name',
    possessive: true
  });
  const districtName = isAtLarge ? 'At-large' : ordinal(parseInt(districtNumber, 10));
  return `${stateName} ${districtName} district`;
};
export default cdContestName;