import { useQuery } from "@tanstack/react-query";
import { useLocation } from "react-router-dom";

import { wwwAXIOS } from "src/Www";
import { GetAvatarLastsAPI } from "src/wwwAPI";
import { toDate } from "src/Utility";

export default function useGetAvatarLasts(
  inputMode: "6K" | "5K" | "7K" | "9K" | "10K" | "14K" | "24K" | "48K",
  avatarID?: string,
) {
  const { pathname } = useLocation();

  return useQuery({
    enabled: !!avatarID && pathname.startsWith("/qwilight/avatar"),
    queryKey: ["avatarLasts", inputMode, avatarID],
    queryFn: async () => {
      const { data } = await wwwAXIOS.get<GetAvatarLastsAPI>(
        `/avatar/lasts/${inputMode}`,
        {
          params: { avatarID },
        },
      );
      return data.map((data) => ({ ...data, fittedText: toDate(data.date) }));
    },
    initialData: [],
  });
}
