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

import { useConfig } from "@hooks/useConfig";
import { bungeeApiFetcher } from "@utils/fetcher";
import type { Token } from "../../Providers/ConfigProvider";

export type ImportTokenResponse = {
	[chainId: number]: Token[];
};

export const useImportToken = ({
	tokenAddress,
	shouldFetch,
}: { tokenAddress: string; shouldFetch: boolean }) => {
	const { baseUrl, apiKey } = useConfig();
	// biome-ignore format: some error with biome I'm gussing
	const query = `${baseUrl}/import-token?tokenAddress=${tokenAddress}`

	const { data, isLoading } = useQuery({
		queryKey: ["bungee-import-token", query, apiKey, tokenAddress, shouldFetch],
		queryFn: () => bungeeApiFetcher(query, apiKey, undefined, "POST"),
		refetchOnWindowFocus: false,
		refetchOnMount: false,
		retry: 2,
		enabled: !!tokenAddress && isAddress(tokenAddress) && shouldFetch,
	});

	return {
		data: data?.result as ImportTokenResponse,
		isLoading,
	};
};
