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

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

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

	// biome-ignore format:
	let query = `${baseUrl}/tokens`;

	if (short) query += "?isShortListed=true";
	// biome-ignore format:
	if (address) query += `&address=${address}`

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

	return data?.result;
};
