import { useSiteStore } from "@/state/Stores";
import { GetSitesAPI } from "@/type/wwwAPI";
import { getSiteName } 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 useGetSites() {
  const { isSiteWindowOpened } = useSiteStore();

  const t = useTranslations();

  const isPath = useIsPath();

  return useQuery({
    enabled: isSiteWindowOpened && isPath("/site"),
    queryKey: ["sites"],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetSitesAPI>("/sites");
      return data.map(
        ({ siteID, siteName, siteConfigure, hasCipher, avatarCount }) => ({
          siteID,
          siteName: getSiteName(siteName, t),
          siteConfigure,
          hasCipher,
          avatarCount,
        }),
      );
    },
    initialData: [],
  });
}
