import { useQuery } from "@tanstack/react-query";
import { wwwAXIOS } from "src/Www";
import { GetAutoEssayAPI } from "src/wwwAPI";
import { getMillis } from "taehui-ts/date";
import { useAvatarStore } from "src/Stores";

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

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