import { GetAvatarLastsAPI } from "@/type/wwwAPI";
import { wwwAPI } from "@/utilities/wwwAPI";
import { useQuery } from "@tanstack/react-query";
import { getDatetime } from "taehui-lib/date";
import { useIsPath } from "taehui-lib/fe-utilities";

export default function useGetAvatarLasts(
  lastsTabItem: string,
  avatarID?: string,
) {
  const isPath = useIsPath();

  return useQuery({
    enabled: !!avatarID && isPath("/avatar"),
    queryKey: ["avatarLasts", lastsTabItem, avatarID],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetAvatarLastsAPI>(
        `/avatar/lasts/${lastsTabItem}`,
        {
          params: { avatarID },
        },
      );
      return data.map((data) => ({
        ...data,
        fittedText: getDatetime(data.date),
      }));
    },
    initialData: [],
  });
}
