import { networkRegistry as e } from "../../../constants/chains.constants.mjs";
import { TRON_DEFAULT_FEE_LIMIT_SUN as t, zeroAddress as n } from "../../../constants/protocol.constants.mjs";
import { insufficientResourcesErrorCodes as r } from "../../../error-handling/error-codes.constants.mjs";
import { caseInsensitiveEqual as i } from "../../utils/caseInsensitive.utils.mjs";
import { circomDataToArray as a, createTronCallData as o, estimateTronFeeSunWithPadding as s, evmHexToTronBase58Address as c, parseZkCalldata as l, reorderZkCallData as u } from "../../utils/tron.utils.mjs";
import { Logger as d } from "../../../error-handling/logger.mjs";
import { ERC20ABI as f } from "../../../externalABIs/index.mjs";
import { simulateTronTransaction as p } from "../../utils/tronSimulation.utils.mjs";
import { waitForTronErc20Approvals as m } from "./waitForErc20Approvals.mjs";
//#region libs/shared/common/src/functions/web3/functionCalls/transactCallDirectTron.ts
var h = async (h, g, _, v, y, b, x, S, C, w = !0) => {
	let T = h.getTronWeb(), E = c(v.erc20TokenAddress), D = await h.getEthereumAddressByChain(g), { hinkalAddress: O, hinkalABI: k } = e[g].contractData, A = T.contract(k, c(O)), j = S ?? A, M = C ?? A;
	if (!j.address) throw Error("contractForApproval address not set");
	if (!M.address) throw Error("contractForTransaction address not set");
	let N = i(v.erc20TokenAddress, n);
	if (!N && _ > 0n) {
		let e = j.address;
		if (!e) throw Error("token approval spender not set");
		let n = _, r = await T.contract(f, E).allowance(D, e).call({ from: D });
		if (BigInt(String(r)) < n) {
			let { transaction: r } = await T.transactionBuilder.triggerSmartContract(E, "approve(address,uint256)", { feeLimit: t }, [{
				type: "address",
				value: e
			}, {
				type: "uint256",
				value: n.toString()
			}], D), i = await T.trx.sign(r), a = await T.trx.sendRawTransaction(i);
			if (!a.result) {
				let e = a.code ?? "UNKNOWN", t = a.message ? Buffer.from(a.message, "hex").toString() : "no message";
				throw Error(`Tron token approval failed: ${e} - ${t}`);
			}
			await m(T, D, e, [{
				tokenAddress: E,
				requiredAmount: n
			}]);
		}
	}
	let { a: P, b: F, c: I } = l(y), L = await u(!0, y, x, b, !1);
	if (!L) throw Error("Tron proof signature not found");
	let R = [
		L.v,
		L.r,
		L.s
	], z = [
		x.tokenNumber,
		x.nullifierAmount,
		x.outputAmount
	], B = a(b);
	if (w && (await p(g, M.address, "transact", M.abi, [
		R,
		P,
		F,
		I,
		z,
		B
	], D, N ? _ : 0n), N)) {
		let e = BigInt(await T.trx.getBalance(D)), n = BigInt(t);
		try {
			let e = o(M.abi, "transact", [
				R,
				P,
				F,
				I,
				z,
				B
			]);
			n = await s(T, g, M.address, D, e, _);
		} catch (e) {
			d.error("Error estimating Tron transact fee", e);
		}
		if (e < _ + n) throw Error(r.INSUFFICIENT_TRON_BALANCE_FOR_FEE);
	}
	let V = await M.transact(R, P, F, I, z, B).send({
		feeLimit: t,
		callValue: N ? Number(_) : 0
	}), H = typeof V == "string" ? V : V?.txid ?? V?.transaction?.txID ?? V?.transaction?.txid;
	if (!H) throw Error("Tron transact failed (missing txid)");
	return H;
};
//#endregion
export { h as transactCallDirectTron };
