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

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

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

	// biome-ignore format:
	const query = `${baseUrl}/bungee-manual/build-tx?routeId=${routeId}`;

	const { data, isLoading, isError, error } = useQuery({
		queryKey: ["build-transaction", query],
		queryFn: () => bungeeApiFetcher(query, apiKey),
		refetchOnWindowFocus: false,
		refetchOnMount: false,
		retry: 2,
		staleTime: 60000,
		enabled: Boolean(routeId),
	});

	return { data: data?.result as BuildTxData, isLoading, error, isError };
};

interface BuildTxData {
	chainId: number;
	txData: string;
	txTarget: string;
	txType: string;
	userTxIndex: number;
	userTxType: "fund-movr";
	value: string;
}
