import { useContestStatus, useCandidatesSortedByResults, useContestMetadata, useContestSummaryResults } from "../../../hooks/election/index.js";
const useCandidatesToShow = contestId => {
  const allCandidates = useCandidatesSortedByResults(contestId);
  const contestSummary = useContestSummaryResults(contestId);
  const contestMeta = useContestMetadata(contestId);
  const contestStatus = useContestStatus(contestId);
  if (!allCandidates || !contestStatus || !contestMeta || !contestSummary) {
    return {
      candidates: [],
      additionalContext: undefined
    };
  }
  const {
    tabulation,
    outcome,
    recount,
    outcomeBalanceType
  } = contestStatus;
  const {
    isUncontested
  } = contestMeta;
  if (tabulation === 'before') {
    return {
      candidatesToShow: [],
      layout: 'too-early'
    };
  }
  if (tabulation === 'noData') {
    return {
      candidatesToShow: [],
      layout: 'no-data'
    };
  }
  if (recount === 'recount') {
    return {
      candidatesToShow: [],
      additionalContext: 'Goes to recount',
      layout: 'recount'
    };
  }
  const numWinners = contestMeta.numWinners || 1;
  const {
    calls
  } = contestSummary;
  if (outcome === 'winner') {
    const winnerCalls = calls.filter(call => call.type === 'winner');
    const winners = winnerCalls.map(call => call.subjectId);
    if (outcomeBalanceType === 'flip') {
      return {
        candidatesToShow: winners,
        layout: 'flip'
      };
    }
    return {
      candidatesToShow: winners,
      layout: 'winner'
    };
  }
  if (outcome === 'runoff') {
    const runoffCalls = calls.filter(call => call.type === 'runoff');
    return {
      candidatesToShow: runoffCalls.map(call => call.subjectId),
      layout: 'runoff'
    };
  }
  if (isUncontested) {
    return {
      candidatesToShow: allCandidates,
      layout: 'uncontested'
    };
  }
  return {
    candidatesToShow: allCandidates.slice(0, numWinners + 1),
    layout: 'standard'
  };
};
export default useCandidatesToShow;