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

export default function useWipeTotem() {
  const { taehuiAvatarName, wipeSession, saveTotem } = useTaehuiStore();

  const t = useTranslations();

  return useMutation({
    mutationFn: async ({ totem }: { totem: string }) => {
      const { data } = await wwwAPI.delete("/avatar/totem", {
        headers: {
          millis: getMillis(),
          totem,
        },
      });
      return data;
    },
    onSuccess: async () => {
      toast.success(t("notLoggedInText", { avatarName: taehuiAvatarName }));
      wipeSession();
      saveTotem();
    },
  });
}
