import { selectorFamily } from 'recoil';
import lodashGet from 'lodash/get';
import { formattedContestCallEventsFromCalls } from '../utils';
import contestSummaryResults from './contestSummaryResults';

/**
 * Contest call events can be hydrated by a calls file.
 */
const contestCallEvents = selectorFamily({
  key: 'election.selectors.contestCallEvents',
  get: key => ({
    get
  }) => {
    const summary = get(contestSummaryResults(key));
    const callList = lodashGet(summary, 'data.calls', []);
    return {
      data: formattedContestCallEventsFromCalls(key, callList),
      source: lodashGet(summary, 'source')
    };
  }
});
export default contestCallEvents;