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

/**
 * Utility to format names of Presidential contests
 *
 * @param {Object} metadata
 * @param {Object} opts
 * @param {string} opts.format
 * @returns {string}
 */
const presContestName = function presContestName(metadata, opts = {}) {
  const {
    stateFips,
    districtNumber
  } = metadata;
  const {
    format = 'long'
  } = opts;
  if (format === 'tiny') {
    if (districtNumber) {
      return ordinal(parseInt(districtNumber, 10));
    }
    return getStateName(stateFips, {
      format: 'abbr'
    });
  }

  // e.g., "NM, Pres.", "ME-01, Pres."
  if (format === 'short') {
    const stateName = getStateName(stateFips, {
      format: 'abbr'
    });
    if (districtNumber) {
      return `${stateName}-${districtNumber}, Pres.`;
    }
    return `${stateName}, Pres.`;
  }

  // e.g., "New Mexico, Pres.", "Maine's 1st District, Pres."
  if (format === 'medium') {
    if (districtNumber) {
      const possesiveStateName = getStateName(stateFips, {
        format: 'name',
        possessive: true
      });
      const districtName = ordinal(parseInt(districtNumber, 10));
      return `${possesiveStateName} ${districtName} district, Pres.`;
    }
    const stateName = getStateName(stateFips, {
      format: 'name'
    });
    return `${stateName}, Pres.`;
  }

  // e.g., "President"
  if (format === 'no-state') {
    return 'President';
  }
  if (districtNumber) {
    const possesiveStateName = getStateName(stateFips, {
      format: 'name',
      possessive: true
    });
    const districtName = ordinal(parseInt(districtNumber, 10));
    return `${possesiveStateName} ${districtName}, President`;
  }
  const stateName = getStateName(stateFips, {
    format: 'name'
  });
  return `${stateName}, President`;
};
export default presContestName;