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

/**
 * A candidate's ballot polarity.
 *
 * This component takes a candidate id and returns "Yes" or "No", depending on
 * whether the option would, or would not, enact the policy at hand.
 *
 * If you pass an id for a candidate that does not have a polarity
 * (most likely because it is not a ballot measure option), the
 * component will return null.
 */
const CandidatePolarity = function CandidatePolarity(props) {
  const {
    id,
    format = 'full'
  } = props;
  const metadata = useCandidateMetadata(id);
  const name = getFormattedCandidatePolarity(metadata, {
    format
  });
  if (!name) return null;
  return <>
      {name}
    </>;
};
CandidatePolarity.propTypes = {
  /**
   * The ID of the candidate (ballot measure option) whose polarity you want to display.
   *
   * If the candidate does not have ballot polarity the component returns null.
   */
  id: PropTypes.string.isRequired,
  /**
   * The format to use.
   */
  format: PropTypes.oneOf(['full', 'parenthesis'])
};
export default CandidatePolarity;