import { useMutation, useQueryClient } from "@tanstack/react-query";
import { wwwAXIOS } from "src/Www";
import { getMillis } from "taehui-ts/date";
import { useAvatarStore, useForumStore } from "src/Stores";

export default function useWipeAutoEssay() {
  const { autoEssayID, setAutoEssayID } = useForumStore();

  const { totem } = useAvatarStore();

  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async () => {
      await wwwAXIOS.delete(`/autoEssay/${autoEssayID}`, {
        headers: { millis: getMillis(), totem },
      });
    },
    onSuccess: async () => {
      setAutoEssayID(undefined);
      await queryClient.invalidateQueries({ queryKey: ["autoEssay"] });
    },
  });
}
