import { isClientFault, wwwAPI } from "@/utilities/wwwAPI";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { toast } from "react-toastify";
import { getMillis } from "taehui-lib/date";

export default function useWipeComment() {
  const queryClient = useQueryClient();

  const t = useTranslations();

  return useMutation({
    mutationFn: async ({
      commentID,
      totem,
    }: {
      commentID: number;
      totem: string;
    }) => {
      await wwwAPI.delete(`/comment/${commentID}`, {
        headers: {
          millis: getMillis(),
          totem,
        },
      });
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["comment"] });
    },
    onError: (e) => {
      if (isClientFault(e)) {
        toast.error(t("failedValidateCipher"));
      }
    },
  });
}
