import { zeroAddress as e } from "../../../constants/protocol.constants.mjs";
import { ContractType as t } from "../../../types/ethereum-network.types.mjs";
import { waitLittle as n } from "../../utils/time.utils.mjs";
import { getContractAddress as r } from "../../utils/getContractAddress.mjs";
import { recoverTransactionFromError as i } from "./recoverTransactionFromError.mjs";
import { approveTokensToHinkal as a } from "./approveTokensToHinkal.mjs";
import { buildApproveAndTransactCalls as o } from "./constructBatchCall.mjs";
import { waitForErc20Approvals as s } from "./waitForErc20Approvals.mjs";
import { ethers as c } from "ethers";
//#region libs/shared/common/src/functions/web3/functionCalls/transactCallDirect.ts
var l = async (l, u, d, f, p, m, h, g, _, v = !0, y = !1) => {
	let b = Array.isArray(d) ? d : [d], x = Array.isArray(f) ? f : [f];
	if (b.length !== x.length) throw Error("token and amount length mismatch");
	let S = l.getContractWithSigner(u, t.HinkalContract), C = await l.getEthereumAddressByChain(u), w = g ?? S, T = _ ?? S, E = "transact", D = x.findIndex(({ erc20TokenAddress: t }) => t === e), O = x.some(({ erc20TokenAddress: t }) => t !== e), k = D === -1 ? 0n : b[D], A = l.getProviderAdapter(u), j = await A.supportsBatchCall?.(u);
	if (!y && O && j && A.sendBatchCallsTransaction) {
		let e = o(x.map((e) => e.erc20TokenAddress), b, r(w), T, p, h, m, k);
		return await A.sendBatchCallsTransaction(u, e);
	}
	if (!y && (await a(l, u, w, x, b), O)) {
		let e = x.map(({ erc20TokenAddress: e }, t) => ({
			tokenAddress: e,
			requiredAmount: b[t]
		}));
		await s(l, u, C, r(w), e);
	}
	let M = {
		value: k > 0n ? BigInt(k).toString() : void 0,
		gasLimit: void 0
	}, N, P = [
		p[0],
		p[1],
		p[2],
		h,
		m,
		{
			...M,
			from: C
		}
	], { runner: F } = T;
	if (!(F instanceof c.AbstractSigner)) throw Error("expected signer");
	if (v) try {
		N = Number(await T[E].estimateGas(...P));
	} catch (e) {
		console.log("hinkalDeposit: gas estimation error", { err: e });
	}
	let I = N && N >= 0 ? Math.ceil(N * 12 / 10) : void 0;
	I && (P[5].gasLimit = I), await n();
	let L;
	try {
		L = y ? await T[E].populateTransaction(...P) : await T[E](...P);
	} catch (e) {
		if (!e?.transactionHash) throw e;
		let t = await i(F.provider, e.transactionHash);
		if (!t) throw e;
		L = t;
	}
	return L;
};
//#endregion
export { l as transactCallDirect };
