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