import { useQuery } from "@tanstack/react-query";
import { wwwAXIOS } from "src/Www";
import { EssayAPIEssay, GetEssayAPI } from "src/wwwAPI";
import { getLanguage } from "taehui-ts/language";

export default function useGetEssay(essayID: string) {
  return useQuery<{
    forumTitle: string;
    title: string;
    text: string;
    date: string;
    avatarID: string;
    avatarName: string;
    hitCount: number;
    essayBefore: EssayAPIEssay | undefined;
    essayLater: EssayAPIEssay | undefined;
  }>({
    queryKey: ["essay", essayID],
    queryFn: async () => {
      const { data } = await wwwAXIOS.get<GetEssayAPI>(`/essay/${essayID}`, {
        params: { language: getLanguage() },
      });
      return data;
    },
    initialData: {
      forumTitle: "",
      title: "",
      text: "",
      date: "",
      avatarID: "",
      avatarName: "",
      hitCount: 0,
      essayBefore: undefined,
      essayLater: undefined,
    },
  });
}
