import { selectorFamily } from 'recoil';
import contest from './contest';

/**
 * Summary results for a single candidate in a contest.
 */
const candidateSummaryResults = selectorFamily({
  key: 'election.selectors.candidateSummaryResults',
  get: key => ({
    get
  }) => {
    if (!key) return undefined;
    const contestId = key.split('::')[1];
    const contestResults = get(contest(contestId));
    if (!contestResults) return undefined;
    return {
      metadata: contestResults.metadata,
      data: contestResults.data.candidateResults[key]
    };
  }
});
export default candidateSummaryResults;