import PropTypes from 'prop-types';
import useContestMetadata from "../../../hooks/election/useContestMetadata/index.js";
import { getContestType } from "../../../utils/election/index.js";
import ContestParty from '../ContestParty';

/**
 * The type of a contest.
 *
 * This component takes a contest ID and renders the contest's type. Note that
 * only certain contest types are actually called out explicitly.
 */
const ContestType = function ContestType(props) {
  const {
    id,
    format
  } = props;
  const metadata = useContestMetadata(id);
  const name = getContestType(metadata, {
    format
  });
  if (!name) return null;
  if (format === 'party-only') {
    return <ContestParty id={id} format="abbr" />;
  }
  return <>
      {name}
    </>;
};
ContestType.defaultProps = {
  format: 'full'
};
ContestType.propTypes = {
  /**
   * The ID of the contest
   */
  id: PropTypes.string.isRequired,
  /**
   * The format to use
   */
  format: PropTypes.oneOf(['full', 'party-only'])
};
export default ContestType;