import { useMemo } from 'react';
import { getCountyStatus } from "../../../utils/election/index.js";
import useContestCandidateColors from '../useContestCandidateColors';
import useContestCountyResults from '../useContestCountyResults';
const useContestCountyMapColors = function useContestCountyMapColors(contestId) {
  const candidateColors = useContestCandidateColors(contestId);
  const countyData = useContestCountyResults(contestId);
  const {
    counties = []
  } = countyData || {};
  return useMemo(() => {
    const statuses = counties.reduce((status, county) => {
      const {
        id,
        fips
      } = county;
      const {
        status: countyStatus,
        candidateId
      } = getCountyStatus(county);
      const palette = candidateColors && candidateColors[candidateId];
      status.fipsToPalette.set(fips, {
        id,
        status: countyStatus,
        candidateId,
        palette
      });
      if (palette) {
        status.palettes.set(candidateId, palette);
      }
      return {
        ...status,
        includeNoClearLeader: status.includeNoClearLeader || countyStatus === 'unclear'
      };
    }, {
      fipsToPalette: new Map(),
      palettes: new Map(),
      includeNoClearLeader: false
    });
    return {
      ...statuses,
      palettes: [...statuses.palettes].map(([candidateId, palette]) => ({
        candidateId,
        palette
      }))
    };
  }, [contestId, candidateColors, counties]);
};
export default useContestCountyMapColors;