import formatNumber from '../formatNumber';

/**
 * Get a candidate's votes from their current results formatted either as
 * a percentage of total votes in the contest or as a raw vote count. This
 * function returns a formatted string.
 *
 * @param {CandidateResults} candidate
 *   The current results of the candidate whose votes you want. If no results
 *   are provided this function returns `undefined`.
 * @param {Object} [options]
 *   Additional configuration options:
 *   - `format`: The format of the name (either `number`, `percent`, or
 *     `sentence`, with `number` being the default)
 * @returns {string}
 */
const getFormattedCandidateVotes = function getFormattedCandidateVotes(candidate = {}, options = {}) {
  const {
    format = 'number',
    ...restOpts
  } = options;
  const {
    votes,
    votesPercent
  } = candidate;
  const voteNum = format === 'percent' ? votesPercent : votes;
  return formatNumber(voteNum, {
    format,
    ...restOpts
  });
};
export default getFormattedCandidateVotes;