import contestMetadata from '../../atoms/contestMetadata';
import candidateMetadata from '../../atoms/candidateMetadata';
import contestSummaryCollection from '../../atoms/contestSummaryCollection';
import contestSummaryResults from '../../atoms/contestSummaryResults';
import contestCountyResults from '../../atoms/contestCountyResults';
import multiSourceMetadata from '../../atoms/multiSourceMetadata';
import calendar from '../../atoms/calendar';
import storyCollection from '../../atoms/storyCollection';
import pagesManifest from '../../atoms/pagesManifest';
import currentPage from '../../atoms/currentPage';
import cycleCandidateMetadata from '../../atoms/cycleCandidateMetadata';
import delegates from '../../atoms/delegates';
const updateAtomFamily = function updateAtomFamily({
  set,
  key,
  ids,
  value,
  atomFamily
}) {
  ids.forEach(id => {
    const atom = atomFamily(id);
    set(multiSourceMetadata(atom.key), oldValue => [...oldValue.filter(source => source.key !== key), value]);
  });
};
const updateAtomSources = function updateAtomSources({
  set,
  key,
  source
}) {
  if (!source || !source.data) return undefined;
  const {
    config: {
      type
    },
    metadata: {
      lastUpdated
    } = {},
    data: newData
  } = source;
  const value = {
    key,
    lastUpdated
  };
  const updateArgs = {
    set,
    key,
    value
  };
  if (type === 'metadata') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: contestMetadata,
      ids: [newData.id]
    });
  }
  if (type === 'metadata-collection') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: contestMetadata,
      ids: Object.keys(newData)
    });
  }
  if (type === 'candidate') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: candidateMetadata,
      ids: [newData.id]
    });
  }
  if (type === 'candidate-collection') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: candidateMetadata,
      ids: Object.keys(newData)
    });
  }
  if (type === 'cycle-candidate-collection') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: cycleCandidateMetadata,
      ids: Object.keys(newData)
    });
  }
  if (type === 'combined-metadata') {
    updateAtomFamily({
      ...updateArgs,
      atomFamily: contestMetadata,
      ids: [newData.contest.id]
    });
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: candidateMetadata,
      ids: Object.keys(newData.candidates)
    });
  }
  if (type === 'combined-metadata-collection') {
    updateAtomFamily({
      ...updateArgs,
      atomFamily: contestMetadata,
      ids: newData.map(d => d.contest.id)
    });
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: candidateMetadata,
      ids: newData.map(d => Object.keys(d.candidates)).flat()
    });
  }
  if (type === 'summary') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: contestSummaryResults,
      ids: [newData.id]
    });
  }
  if (type === 'summary-collection') {
    updateAtomFamily({
      ...updateArgs,
      atomFamily: contestSummaryCollection,
      ids: [newData.id]
    });
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: contestSummaryResults,
      ids: newData.contests
    });
  }
  if (type === 'county-collection') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: contestCountyResults,
      ids: [newData.id]
    });
  }
  if (type === 'calendar') {
    set(multiSourceMetadata(calendar.key), oldValue => [...oldValue.filter(s => s.key !== key), value]);
  }
  if (type === 'story-collection') {
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: storyCollection,
      ids: [newData.tag]
    });
  }
  if (type === 'pages-manifest') {
    set(pagesManifest, newData);
  }
  if (type === 'current-page') {
    set(currentPage, newData);
  }
  if (type === 'delegates') {
    const ids = new Set(newData.parties.map(d => d.party));
    return updateAtomFamily({
      ...updateArgs,
      atomFamily: delegates,
      ids: [...ids]
    });
  }
  return undefined;
};
export default updateAtomSources;