import get from 'lodash/get';
import getOfficeName from "../getOfficeName/index.js";
import autohed from './autohed';
import getLeaderParty from './getLeaderParty';
const bodyShortHeadline = function bodyShortHeadline(opts) {
  const {
    office,
    bop,
    customHeadlines = {}
  } = 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 leaderParty = getLeaderParty(bop);
  const isLeader = !!leaderParty;
  const pluralParty = {
    dem: 'Dems',
    gop: 'GOP'
  };
  const partyWinsVerb = {
    dem: 'take',
    gop: 'takes'
  };
  const partyLeadsVerb = {
    dem: 'lead',
    gop: 'leads'
  };
  if (isWinner && pluralParty[winnerParty]) {
    const custom = get(customHeadlines, `${winnerParty}Winner`);
    if (custom) return custom;
    if (office === 'president') {
      return autohed`${winnerLabel} wins the presidency`;
    }
    return autohed`${pluralParty[winnerParty]} ${partyWinsVerb[winnerParty]} ${officeName}`;
  }
  if (isLeader) {
    const custom = get(customHeadlines, `${winnerParty}Leader`);
    if (custom) return custom;
    if (office === 'president') {
      return 'Presidential election results';
    }
    return autohed`${pluralParty[leaderParty]} ${partyLeadsVerb[leaderParty]} ${officeName} race`;
  }
  const custom = get(customHeadlines, 'default');
  if (custom) return custom;
  if (office === 'president') {
    return 'Presidential election results';
  }
  return autohed`${officeName} election results`;
};
export default bodyShortHeadline;