import { useCallback } from "react";
import type { Address } from "viem";
import { useSendTransaction } from "wagmi";

import type { TxData } from "@hooks/apis/common/quote/types";

export function useRegularTransaction({
	txData,
}: {
	txData?: TxData;
}) {
	const {
		sendTransaction,
		sendTransactionAsync,
		isSuccess,
		isError,
		data,
		error,
	} = useSendTransaction();

	const trigger = useCallback(() => {
		if (!txData) return;
		sendTransaction({
			data: txData.data as Address,
			to: txData.to as Address,
			value: BigInt(txData.value),
		});
	}, [txData, sendTransaction]);

	const triggerAsync = useCallback(() => {
		if (!txData) return;
		return sendTransactionAsync({
			data: txData.data as Address,
			to: txData.to as Address,
			value: BigInt(txData.value),
		});
	}, [txData, sendTransactionAsync]);

	return {
		trigger,
		triggerAsync,
		isSuccess,
		isError,
		txData: data,
		error,
	};
}
