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 bodyShortHeadline = function bodyShortHeadline(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, 'winner.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 partyWinsVerb = {
    dem: 'win',
    gop: 'wins'
  };
  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) {
      return autohed`${gopName} and ${demName} tied for now`;
    }
    if (isTied && isFinal) {
      return autohed`Presidential election results`;
    }
    if (!isZero && demName && gopName) {
      return autohed`First results in for ${demName} and ${gopName}`;
    }
    return autohed`Presidential election results`;
  }
  if (office === 'senate') {
    if (isWinner) {
      return autohed`Senate: ${pluralParty[winnerParty]} ${partyWinsVerb[winnerParty]}`;
    }
    if (isTied && isFinal) {
      return `${officeName}: Tied, again`;
    }
    return `${officeName} election results`;
  }
  if (office === 'house') {
    if (isWinner) {
      return autohed`${officeName}: ${pluralParty[winnerParty]} ${partyWinsVerb[winnerParty]}`;
    }
    if (isLeader) {
      return autohed`${officeName}: ${pluralParty[leaderParty]} ${partyLeadsVerb[leaderParty]}`;
    }
    if (isTied && !isFinal && !isEarly) {
      return `${officeName} neck-and-neck`;
    }
    return `${officeName} election results`;
  }
  return autohed`${officeName} election results`;
};
export default bodyShortHeadline;