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

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

export const useManualStatus = ({
	requestHash,
	fromChainId,
}: { requestHash: string; fromChainId: number }) => {
	const { manualApiKey, manualBaseUrl } = useConfig();
	// biome-ignore format:
	const query = `${manualBaseUrl}/bridge-status?transactionHash=${requestHash}&fromChainId=${fromChainId}`;

	const { data, isLoading } = useQuery({
		queryKey: ["manual-status", query, requestHash, fromChainId],
		queryFn: () => bungeeApiFetcher(query, manualApiKey),
		refetchOnMount: false,
		enabled: Boolean(requestHash && fromChainId),
		refetchOnWindowFocus: false,
		retry: 2,
		refetchInterval: 10000,
	});

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

type ManualStatus = {
	destinationTransactionHash: string;
	destinationTxStatus: "PENDING" | "COMPLETED";
	toAmount: string;
};
