import { useQuery } from "@tanstack/react-query";
import { apiGet } from "../../../../lib/api/methods";
import { AUTH_ENDPOINTS } from "../endpoints";
import { useSearchParams } from "next/navigation";

export default function useVerifyEmail(tokenProp?: string) {
  const searchParams = useSearchParams();
  const token = tokenProp ?? searchParams.get("token") ?? undefined;

  const { data, isFetching, isError, error } = useQuery({
    queryKey: ["verify-email", token],
    queryFn: async () => {
      const res = await apiGet(AUTH_ENDPOINTS.VERIFY_EMAIL, { token });
      return res;
    },
    enabled: !!token,
    staleTime: Infinity,
    gcTime: 0,
    retry: false,
  });

  return { data, loading: isFetching, error, isError };
}
