import { isClientFault, wwwAPI } from "@/utilities/wwwAPI";
import { GetCommentaryAPI } from "@/type/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 useWipeCommentary() {
  const queryClient = useQueryClient();

  const t = useTranslations();

  return useMutation({
    mutationFn: async ({
      commentaryID,
      avatarCipher,
    }: {
      commentaryID: number;
      avatarCipher: string;
    }) => {
      await wwwAPI.delete<GetCommentaryAPI>("/commentary", {
        params: {
          commentaryID,
          avatarCipher,
        },
        headers: {
          millis: getMillis(),
        },
      });
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["commentary"] });
    },
    onError: (e) => {
      if (isClientFault(e)) {
        toast.error(t("failedValidateCipher"));
      }
    },
  });
}
