import lodashGet from 'lodash/get';
import { selectorFamily } from 'recoil';
import summaryCollectionContestIds from '../atoms/summaryCollectionContestIds';
import contest from './contest';
const contestCollectionContests = selectorFamily({
  key: 'election.selectors.contestCollectionContests',
  get: key => ({
    get
  }) => {
    const ids = get(summaryCollectionContestIds(key));
    if (!ids) return undefined;
    const contestEntries = ids.data.map(contestId => {
      const results = get(contest(contestId));
      const contestData = lodashGet(results, 'data');
      if (!contestData) return undefined;
      return [contestId, {
        ...contestData
      }];
    }).filter(Boolean);
    return {
      ...ids,
      data: Object.fromEntries(contestEntries)
    };
  }
});
export default contestCollectionContests;