import { useState } from 'react';
import { useCandidatesSortedByResults } from "../../../../hooks/election/index.js";
const useCandidateGroups = function useCandidateGroups({
  id,
  expandable,
  expandableCutoff
}) {
  const [isExpanded, setIsExpanded] = useState(false);
  const sortedCandidates = useCandidatesSortedByResults(id);
  if (expandable === 'aggregationOnly' && sortedCandidates.length > expandableCutoff + 1) {
    /**
     * If the results table is not expandable
     * Show top two, plus aggregated other cands
     */
    return {
      candidatesToShow: sortedCandidates.slice(0, expandableCutoff),
      otherCandidates: sortedCandidates.slice(expandableCutoff),
      showOtherCandidatesRow: true,
      isExpanded,
      setIsExpanded
    };
  }

  /**
   * If it is expandable, but not expanded
   * and there's more than one candidate to hide,
   * show three and aggregate the rest
   */
  if (expandable === 'expandableAggregation' && !isExpanded && sortedCandidates.length > expandableCutoff + 2) {
    return {
      candidatesToShow: sortedCandidates.slice(0, expandableCutoff + 1),
      otherCandidates: sortedCandidates.slice(expandableCutoff + 1),
      showOtherCandidatesRow: true,
      isExpanded,
      setIsExpanded
    };
  }

  /**
   * If it is expandable, and expanded
   * show all candidates, but
   * keep showing  the other candidates row
   */
  if (expandable === 'expandableAggregation' && isExpanded && sortedCandidates.length > expandableCutoff + 2) {
    return {
      candidatesToShow: sortedCandidates,
      otherCandidates: [],
      showOtherCandidatesRow: true,
      isExpanded,
      setIsExpanded
    };
  }

  /**
   * Otherwise, show all candidates
   */
  return {
    candidatesToShow: sortedCandidates,
    otherCandidates: [],
    showOtherCandidatesRow: false,
    isExpanded,
    setIsExpanded
  };
};
export default useCandidateGroups;