import PropTypes from 'prop-types';
import useContestCandidatesMetadata from "../../../hooks/election/useContestCandidatesMetadata/index.js";
import { candidatesIncludeIncumbent } from "../../../utils/election/index.js";

/**
 * A footnote for a contest that has a labeled incumbent.
 *
 * The component will render "* Incumbent" if the contest you passed has an
 * incumbent running. If the contest does not have an incumbent, it will return
 * null.
 *
 * This should be used together with [the `ContestIncumbentLabel`
 * component][candidate-incumbent], which can be used to mark the appropriate
 * candidate with an asterisk.
 *
 * [candidate-incumbent]: ./?path=/story/components-election-candidateincumbentlabel--basic
 */
const ContestIncumbentLabel = function ContestIncumbentLabel(props) {
  const {
    id
  } = props;
  const candidates = useContestCandidatesMetadata(id);
  const hasIncumbent = candidatesIncludeIncumbent(candidates);
  if (!hasIncumbent) {
    return null;
  }
  return <>
      * Incumbent
    </>;
};
ContestIncumbentLabel.defaultProps = {};
ContestIncumbentLabel.propTypes = {
  /**
   * The ID of the contest where you're marking an incumbent
   */
  id: PropTypes.string.isRequired
};
export default ContestIncumbentLabel;