import aggregateCandidateVotes from '../aggregateCandidateVotes';

/**
 * Aggregate the percentage of total votes that multiple candidates have won in
 * a single contest. The first argument is the summary results for the contest.
 * The second optional argument is an array of candidate IDs that should be
 * included in the aggregation. If you don't provide an array of candidate IDs
 * then all candidates are included in the aggregation, meaning the aggregate
 * percentage will be 100%, represented numerically as the number `1`.
 *
 * @param {ContestSummaryResults} summary
 *   The summary results of the contest to aggregate
 * @param {CandidateId[]} [candidateIds]
 *   An optional array of specific candidate IDs to include in the aggregation.
 *   If no array is provided then all candidates are included (the default).
 * @returns {number}
 */
const aggregateCandidateVotePercents = function aggregateCandidateVotePercents(summary, candidateIds) {
  if (!summary) return undefined;
  const {
    totalVotes = 0
  } = summary;
  const candidateVotes = aggregateCandidateVotes(summary, candidateIds);
  if (totalVotes === 0) return 0;
  return candidateVotes / totalVotes;
};
export default aggregateCandidateVotePercents;