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

import { useConfig } from "@hooks/useConfig";
import { bungeeApiFetcher } from "@utils/fetcher";

export const useFetchTokens = ({
	address,
}: {
	address?: string;
}) => {
	const { apiKey, baseUrl } = useConfig();

	// biome-ignore format:
	let query = `${baseUrl}/tokens/list`;
	// biome-ignore format:
	if (address) query += `?userAddress=${address}`;

	const { data } = useQuery({
		queryKey: ["bungee-supported-tokens", apiKey, address],
		queryFn: () => bungeeApiFetcher(query, apiKey),
		refetchOnWindowFocus: false,
		retry: 2,
		staleTime: 20000, // 20 seconds
	});

	return data?.result;
};
