import { GetAvatarAbilitiesAPI } from "@/type/wwwAPI";
import { formatText } from "@/utilities/Utility";

import { wwwAPI } from "@/utilities/wwwAPI";
import { useQuery } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { useIsPath } from "taehui-lib/fe-utilities";

export default function useGetAvatarAbility(
  abilitiesTabItem: string,
  avatarID?: string,
) {
  const t = useTranslations();

  const isPath = useIsPath();

  return useQuery({
    enabled: !!avatarID && isPath("/avatar"),
    queryKey: ["avatarAbility", abilitiesTabItem, avatarID],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetAvatarAbilitiesAPI>(
        `/avatar/ability/${abilitiesTabItem}`,
        {
          params: { avatarID },
        },
      );
      return data.map((data) => ({
        ...data,
        fittedText: t("abilityFittedText", {
          stand: formatText(data.stand),
          ability: formatText(data.ability),
        }),
      }));
    },
    initialData: [],
  });
}
