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

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

export const useTokenSearch = ({
	keyword,
	address,
}: {
	keyword: string;
	address: string;
}) => {
	const { baseUrl, apiKey } = useConfig();
	// biome-ignore format:
	const query = `${baseUrl}/tokens/search?q=${keyword}&address=${address}`;

	const { data, isLoading } = useQuery({
		queryKey: ["token-search", query, apiKey],
		queryFn: () => bungeeApiFetcher(query, apiKey),
		refetchOnWindowFocus: false,
		refetchOnMount: false,
		retry: 2,
		staleTime: 300000, // 5 minutes
		enabled: keyword?.length > 1,
	});

	return { data: data?.result.tokens, isLoading };
};
