import { useMutation, useQueryClient } from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";

export const useDeleteGrocery = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationKey: ["deleteGrocery"],
    mutationFn: async (groceryId: string) => {
      try {
        await firestore().collection("Groceries").doc(groceryId).delete();
      } catch (e) {
        console.error(e);
        throw new Error("Failed to delete the grocery item.");
      }
    },
    onSuccess: () => {
      queryClient.invalidateQueries({queryKey: ["groceries"]});
    },
  });
};
