import { useContestCollectionContests } from "../../../hooks/election/index.js";

// TO DO: flips

const useContestCollectionBopConfig = collectionId => {
  const contests = useContestCollectionContests(collectionId);
  if (!contests) {
    return undefined;
  }
  return Object.keys(contests).reduce((res, curr) => {
    const {
      contestStatus: {
        outcome,
        winningParty
      }
    } = contests[curr];
    const resBop = res.bop.bop;
    const winner = outcome === 'winner';
    if (winner && winningParty === 'dem') {
      return {
        ...res,
        bop: {
          bop: {
            ...resBop,
            calledDem: resBop.calledDem + 1
          }
        }
      };
    }
    if (winner && winningParty === 'gop') {
      return {
        ...res,
        bop: {
          bop: {
            ...resBop,
            calledGop: resBop.calledGop + 1
          }
        }
      };
    }
    if (winner) {
      return {
        ...res,
        bop: {
          bop: {
            ...resBop,
            calledOtherParty: resBop.calledOtherParty + 1
          }
        }
      };
    }
    return {
      ...res,
      bop: {
        bop: {
          ...resBop,
          notCalled: resBop.notCalled + 1
        }
      }
    };
  }, {
    mode: 'segmented',
    labelFormat: 'count',
    disableFlipCount: false,
    bop: {
      bop: {
        calledDem: 0,
        calledGop: 0,
        calledOtherParty: 0,
        notCalled: 0
      }
    }
  });
};
export default useContestCollectionBopConfig;