import contestTypes from "../constants/contestTypes.js";
import getPartyName from '../getPartyName';

/**
 * Get a contest's type.
 *
 * @param {ContestMetadata} contest
 *   The metadata of the contest for which you want the type. If no metadata is
 *   provided or if the metadata doesn't contain the appropriate type
 *   information, this function returns `undefined`.
 * @param {Object} [options]
 *   Additional configuration options:
 *   - `format`: The format of the name (either `full`, `party-only`
 *      with `full` being the default)
 * @returns {string}
 */

const getContestType = function getContestType(contest, options = {}) {
  if (!contest) return undefined;
  const {
    contestType,
    contestTypeName,
    party
  } = contest;
  const {
    format = 'full'
  } = options;
  if (typeof contestTypeName !== 'undefined') return contestTypeName;
  if (format === 'party-only') return getPartyName(party);
  if (contestType === 'caucus') {
    return `${getPartyName(party)} caucus`;
  }
  return contestTypes[contestType] && contestTypes[contestType][format];
};
export default getContestType;