import PropTypes from 'prop-types';
import { STATE_SIZE, Canvas, StateCounties } from "../../maps/index.js";
import { useContestMetadata, useContestCountyMapColors, useGetMapFills, useSortMapBoundariesCompare } from "../../../hooks/election/index.js";
import useTooltip from "../../../hooks/useTooltip/index.js";
import ElectionMapLegend from "../ElectionMapLegend/index.js";
import CountyTooltip from './CountyTooltip';
import classes from './styles.module.scss';

/**
 * A county-level map of a single state displaying county-level results
 * for a state-wide race.
 */
const StateRaceCountiesMap = function StateRaceCountiesMap(props) {
  const {
    contestId
  } = props;
  const {
    stateFips
  } = useContestMetadata(contestId) || {};
  const colors = useContestCountyMapColors(contestId);
  const getCountyProps = useGetMapFills(colors, 'geo');
  const sortBoundariesCompare = useSortMapBoundariesCompare(colors);
  const tooltip = useTooltip({
    content: CountyTooltip,
    props: {
      contestId,
      colors
    },
    displayProps: {
      unstyled: true
    }
  });
  const mapClasses = {
    mapContainer: classes.mapContainer
  };
  return <div className={classes.container}>
      <Canvas size={STATE_SIZE} zoomable customClasses={mapClasses}>
        {stateFips && <StateCounties stateFips={stateFips} getBoundaryProps={getCountyProps} sortBoundariesCompare={sortBoundariesCompare} tooltip={tooltip} />}
      </Canvas>
      <ElectionMapLegend palettes={colors.palettes} includeNoClearLeader={colors.includeNoClearLeader} labelType="name" />
    </div>;
};
StateRaceCountiesMap.defaultProps = {};
StateRaceCountiesMap.propTypes = {
  /**
   * The ID of the contest
   */
  contestId: PropTypes.string.isRequired
};
export default StateRaceCountiesMap;