import get from 'lodash/get';
import getOfficeName from "../getOfficeName/index.js";
import autohed from './autohed';
import getLeaderParty from './getLeaderParty';
import getIsTied from './getIsTied';
import getIsFinal from './getIsFinal';
import getIsEarly from './getIsEarly';
import getIsZero from './getIsZero';
const bodyHeadline = function bodyHeadline(opts) {
  const {
    office,
    bop,
    customHeadlines = {},
    presidentialCandidates = {}
  } = opts;
  if (office === 'governor') {
    const custom = get(customHeadlines, 'default');
    if (custom) return custom;
    return 'Governor election results';
  }
  const officeName = getOfficeName(office);
  const winnerParty = get(bop, 'winner.party');
  const winnerLabel = get(bop, 'winner.label');
  const isWinner = !!winnerParty;
  const leaderLabel = get(bop, 'leader.label');
  const leaderParty = getLeaderParty({
    bop,
    office
  });
  const isLeader = !!leaderParty;
  const isEarly = getIsEarly({
    bop,
    office
  });
  const isTied = getIsTied(bop);
  const isFinal = getIsFinal(bop);
  const isZero = getIsZero(bop);
  const pluralParty = {
    dem: 'Dems',
    gop: 'GOP'
  };
  const pluralPartyLong = {
    dem: 'Democrats',
    gop: 'Republicans'
  };
  const partyLeadsVerb = {
    dem: 'lead',
    gop: 'leads'
  };
  const customWinnerHed = get(customHeadlines, `${winnerParty}Winner`);
  if (isWinner && customWinnerHed) {
    return customWinnerHed;
  }
  const customLeaderHed = get(customHeadlines, `${leaderParty}Leader`);
  if (isLeader && customLeaderHed) {
    return customLeaderHed;
  }
  const customZeroHed = get(customHeadlines, 'zero');
  if (isZero && customZeroHed) {
    return customZeroHed;
  }
  const customDefaultHed = get(customHeadlines, 'default');
  if (customDefaultHed) {
    return customDefaultHed;
  }
  if (office === 'president') {
    const demName = get(presidentialCandidates, 'dem');
    const gopName = get(presidentialCandidates, 'gop');
    if (isWinner) {
      const name = get(presidentialCandidates, winnerParty, winnerLabel);
      return autohed`${name} wins the presidency`;
    }
    const leaderName = get(presidentialCandidates, leaderParty, leaderLabel);
    if (isLeader && leaderName) {
      return autohed`${leaderName} ahead in the Electoral College`;
    }
    if (isTied && !isFinal && demName && gopName && !isEarly) {
      return autohed`${gopName} and ${demName} tied for now`;
    }
    if (!isZero) {
      return autohed`All eyes on the battlegrounds`;
    }
    return autohed`Presidential election results`;
  }
  if (office === 'senate') {
    if (isWinner) {
      return autohed`${pluralPartyLong[winnerParty]} win the ${officeName}`;
    }
    if (isTied && isFinal) {
      return 'We’ve got a 50-50 Senate';
    }
    return `${officeName} election results`;
  }
  if (office === 'house') {
    if (isWinner) {
      return autohed`${pluralPartyLong[winnerParty]} win the ${officeName}`;
    }
    if (isLeader) {
      return autohed`${pluralParty[leaderParty]} ${partyLeadsVerb[leaderParty]} race for ${officeName} control`;
    }
    if (isTied && !isFinal && !isEarly) {
      return `${officeName} control neck-and-neck`;
    }
    return `${officeName} election results`;
  }
  return autohed`${officeName} election results`;
};
export default bodyHeadline;