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

/**
 * 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 summary = get(contestSummaryResults(contestId));
    if (!summary) return undefined;
    return {
      data: summary.data.results.find(({
        id
      }) => id === key),
      source: summary.source
    };
  }
});
export default candidateSummaryResults;