import { selectorFamily } from 'recoil';
import lodashGet from 'lodash/get';
import getMostRecentSource from '../utils/getMostRecentSource';
import candidateMetadata from './candidateMetadata';
import cycleCandidateMetadata from './cycleCandidateMetadata';
const compoundCandidateMetadata = selectorFamily({
  key: 'election.selectors.compoundCandidateMetadata',
  get: key => ({
    get
  }) => {
    if (!key) return undefined;
    if (key.startsWith('cycle')) {
      return get(cycleCandidateMetadata(key));
    }
    const meta = get(candidateMetadata(key));
    if (!meta) return undefined;
    const cycleMeta = meta.data.cycleId ? get(cycleCandidateMetadata(meta.data.cycleId)) : {};
    return {
      data: {
        ...meta.data,
        ...lodashGet(cycleMeta, 'data', {}),
        id: meta.data.id
      },
      source: getMostRecentSource([lodashGet(cycleMeta, 'source', undefined), meta.source])
    };
  }
});
export default compoundCandidateMetadata;