import { useQuery } from "@tanstack/react-query";
import { getLanguage } from "taehui-ts/language";

import { useSiteStore } from "src/Stores";
import { wwwAXIOS } from "src/Www";
import { GetCommentAPI } from "src/wwwAPI";

export default function useGetComment(
  noteID: string,
  isCommentOpened: boolean,
) {
  const { siteAvatarID } = useSiteStore();

  const language = getLanguage();
  return useQuery({
    enabled: isCommentOpened,
    queryKey: ["comment", noteID, siteAvatarID, language],
    queryFn: async () => {
      const { data } = await wwwAXIOS.get<GetCommentAPI>("/comment", {
        params: {
          noteID,
          avatarID: siteAvatarID,
          language,
        },
      });

      return data;
    },
    initialData: {
      comments: [],
      commentPlace: -1,
      totalComments: 0,
    },
  });
}
