import { useMemo } from 'react';
import useFavoriteContests from '../useFavoriteContests';

/**
 * Use the current favorited state of a particular contest. This returns the
 * favorited state of the contest and a toggle function allowing you to switch
 * the contest's state.
 */
const useFavoriteContestToggle = function useFavoriteContestToggle(contestId) {
  const {
    isContestFavorite,
    addFavoriteContest,
    removeFavoriteContest
  } = useFavoriteContests();
  return useMemo(() => {
    const isFavorite = isContestFavorite(contestId);
    return {
      isFavorite,
      toggleFavorite: isFavorite ? () => removeFavoriteContest(contestId) : () => addFavoriteContest(contestId)
    };
  }, [isContestFavorite, addFavoriteContest, removeFavoriteContest]);
};
export default useFavoriteContestToggle;