import { useMemo, useState, useCallback } from 'react';
const useDisplayCandidates = function useDisplayCandidates(opts) {
  const {
    candidates,
    displayCandidates,
    displayCandidatesNumber
  } = opts;
  const [isExpanded, setIsExpanded] = useState(false);
  const toggleExpanded = useCallback(() => {
    setIsExpanded(old => !old);
  }, []);
  const {
    showCandidates,
    hideCandidates
  } = useMemo(() => {
    const includeCandidate = (candidate, i) => {
      if (isExpanded) return true;
      if (displayCandidates === 'all') return true;
      if (typeof displayCandidatesNumber === 'number') {
        return i < displayCandidatesNumber;
      }
      return !candidate.isDropped;
    };
    const toShow = [];
    const toHide = [];
    candidates.forEach((candidate, i) => {
      if (includeCandidate(candidate, i)) {
        toShow.push(candidate);
      } else {
        toHide.push(candidate);
      }
    });
    return {
      showCandidates: toShow,
      hideCandidates: toHide
    };
  }, [candidates, displayCandidates, displayCandidatesNumber, isExpanded]);
  return {
    showCandidates,
    hideCandidates,
    isExpanded,
    setIsExpanded,
    toggleExpanded
  };
};
export default useDisplayCandidates;