import { selectorFamily } from 'recoil';
import combinedMetadata from '../atoms/combinedMetadata';

/**
 * Contest metadata can either be hydrated by a metadata collection or
 * a single-contest metadata file.
 */
const contestMetadata = selectorFamily({
  key: 'election.selectors.contestMetadata',
  get: key => ({
    get
  }) => {
    if (!key) return undefined;
    const source = get(combinedMetadata(key));
    if (!source || !source.data) return undefined;
    return {
      metadata: source.metadata,
      data: source.data.contest
    };
  }
});
export default contestMetadata;