import { useMemo } from 'react';
import sortBy from "../../../utils/sorting/sortBy.js";

/**
 * Memo-ized sort of an array of states by their total number of delegates.
 * @param {string} delegates         - The FIPS code of the state that's highlighted
 * @returns {array}
 */

const useStatesByDelegates = delegates => useMemo(() => {
  if (!delegates) {
    return [undefined, undefined];
  }
  const filteredDelegates = delegates.states.filter(d => d.stateFips !== 'UNKNOWN' && d.total !== 0);
  const sortedDelegates = sortBy(filteredDelegates, [{
    comparison: state => state.total,
    direction: 'desc'
  }, {
    comparison: state => state.stateFips,
    direction: 'desc'
  }]);
  const totalDelegates = delegates.total;
  return [sortedDelegates, totalDelegates];
}, [delegates]);
export default useStatesByDelegates;