import { useTaehuiStore } from "@/state/Stores";
import { GetAutoEssayAPI } from "@/type/wwwAPI";
import { wwwAPI } from "@/utilities/wwwAPI";
import { useQuery } from "@tanstack/react-query";
import { getMillis } from "taehui-lib/date";

export default function useGetAutoEssay(
  forumID: string,
  isAutoEssayOpened: boolean,
) {
  const { totem } = useTaehuiStore();

  return useQuery({
    enabled: isAutoEssayOpened,
    queryKey: ["autoEssay", forumID, totem],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetAutoEssayAPI>(
        `/autoEssay/${forumID}`,
        {
          headers: {
            millis: getMillis(),
            totem,
          },
        },
      );
      return data;
    },
    initialData: [],
  });
}
