import { GetTitleAPI } from "@/type/wwwAPI";
import { wwwAPI } from "@/utilities/wwwAPI";
import { useQuery } from "@tanstack/react-query";
import { useLanguage } from "taehui-lib/language";

export default function useGetTitle(avatarID?: string) {
  const language = useLanguage();

  return useQuery({
    enabled: !!avatarID,
    queryKey: ["title", avatarID, language],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetTitleAPI>("/title", {
        params: {
          avatarID,
          language,
        },
      });
      if (data.titleColor.startsWith("#")) {
        return {
          ...data,
          titleColor: `#${data.titleColor.substring(
            3,
          )}${data.titleColor.substring(1, 3)}`,
        };
      } else {
        return data;
      }
    },
    initialData: {
      title: "",
      titleColor: "",
    },
  });
}
