import { ContractType as e } from "../../types/ethereum-network.types.mjs";
import { isTronLike as t, networkRegistry as n } from "../../constants/chains.constants.mjs";
import { TRON_DEFAULT_FEE_LIMIT_SUN as r, zeroAddress as i } from "../../constants/protocol.constants.mjs";
import { caseInsensitiveEqual as a } from "../../functions/utils/caseInsensitive.utils.mjs";
import { emitTxPublicData as o } from "../../API/admin-calls.mjs";
import { Logger as s } from "../../error-handling/logger.mjs";
import { assertTronSufficientBalance as c, createTronCallData as l, evmHexToTronBase58Address as u, splitTronTokensByNative as d } from "../../functions/utils/tron.utils.mjs";
import { validateAndGetChainId as f } from "../../functions/utils/token-check.utils.mjs";
import { constructAdminData as p } from "../../functions/pre-transaction/constructAdminData.mjs";
import { getStealthAddressStructureFromUserKeys as m } from "../../functions/utils/getRecipientInfoFromUserKeys.mjs";
import { getContractAddress as h } from "../../functions/utils/getContractAddress.mjs";
import { simulateTronTransaction as g } from "../../functions/utils/tronSimulation.utils.mjs";
import { buildApproveAndProoflessDepositCalls as _ } from "../../functions/web3/functionCalls/constructBatchCall.mjs";
import { waitForErc20Approvals as v, waitForTronErc20Approvals as y } from "../../functions/web3/functionCalls/waitForErc20Approvals.mjs";
import { aggregateAmountsForApproval as b, approveTokens as x, approveTronTokens as S, assertNoDuplicateStealthAddressStructures as C } from "./hinkalProoflessDeposit.utils.mjs";
import { hinkalEvmProoflessDepositViaWrapper as w, hinkalTronProoflessDepositViaWrapper as T } from "./hinkalProoflessDepositViaWrapper.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalProoflessDeposit.ts
var E = async (e, t, i, a, o, f = !1) => {
	let p = e.getTronWeb(), { hinkalAddress: m } = n[t].contractData, h = u(m), _ = await e.getEthereumAddressByChain(t), v = i.map((e) => e.erc20TokenAddress), b = i.map(() => 0n), { totalTrxValue: x, tokensNeedingApproval: C } = d(i, a), w = o.map((e) => ({
		extraRandomization: e.extraRandomization.toString(),
		stealthAddress: e.stealthAddress.toString(),
		H0: e.H0.toString(),
		H1: e.H1.toString()
	}));
	if (x > 0n) {
		let { hinkalABI: e } = n[t].contractData;
		if (!e) throw Error("Hinkal ABI not found");
		await c(p, t, h, _, l(e, "prooflessDeposit", [
			v,
			a.map(String),
			b.map(String),
			o.map((e) => [
				e.extraRandomization.toString(),
				e.stealthAddress.toString(),
				e.H0.toString(),
				e.H1.toString()
			])
		]), x);
	}
	let { transaction: T } = await p.transactionBuilder.triggerSmartContract(h, "prooflessDeposit(address[],uint256[],uint256[],(uint256,uint256,uint256,uint256)[])", {
		feeLimit: r,
		callValue: x > 0n ? Number(x) : 0
	}, [
		{
			type: "address[]",
			value: v
		},
		{
			type: "uint256[]",
			value: a.map(String)
		},
		{
			type: "uint256[]",
			value: b.map(String)
		},
		{
			type: "tuple(uint256,uint256,uint256,uint256)[]",
			value: w.map((e) => [
				e.extraRandomization,
				e.stealthAddress,
				e.H0,
				e.H1
			])
		}
	], _);
	if (f) return T;
	await S(p, _, h, C), await y(p, _, h, Array.from(C.entries()).map(([e, t]) => ({
		tokenAddress: e,
		requiredAmount: t
	}))), await g(t, h, "prooflessDeposit", n[t].contractData.hinkalABI, [
		v,
		a.map(String),
		b.map(String),
		o.map((e) => [
			e.extraRandomization.toString(),
			e.stealthAddress.toString(),
			e.H0.toString(),
			e.H1.toString()
		])
	], _, x > 0n ? x : void 0);
	let E = await p.trx.sign(T), D = await p.trx.sendRawTransaction(E);
	if (!D.result) throw s.error("Tron prooflessDeposit broadcast failed", D), Error("Tron prooflessDeposit broadcast failed");
	let O = typeof D == "string" ? D : D?.txid ?? D?.transaction?.txID;
	if (!O) throw Error("Tron prooflessDeposit failed (missing txid)");
	return O;
}, D = async (n, r, s, c, l, u = !1) => {
	let d = f(r), g = t(d), y = await n.getEthereumAddressByChain(d), S = c ?? r.map(() => m(n.userKeys));
	if (S.length !== r.length) throw Error("Stealth address structures length must be equal to erc20 tokens length");
	C(S);
	let w;
	if (g) {
		let e = await E(n, d, r, s, S, u);
		if (u) return e;
		w = e;
	} else {
		let t = n.getContractWithSigner(d, e.HinkalContract), o = [
			r.map(({ erc20TokenAddress: e }) => e),
			s,
			r.map(() => 0n),
			S
		];
		if (u) return t.prooflessDeposit.populateTransaction(...o);
		let c = n.getProviderAdapter(d), l = b(r, s), f = r.reduce((e, t, n) => a(t.erc20TokenAddress, i) ? e + s[n] : e, 0n), p = l.filter(({ token: e, balance: t }) => !a(e.erc20TokenAddress, i) && t > 0n), m;
		if (p.length > 0 && await c.supportsBatchCall?.(d) && c.sendBatchCallsTransaction) {
			let e = _(p.map(({ token: e }) => e.erc20TokenAddress), p.map(({ balance: e }) => e), h(t), t, ...o, f);
			m = await c.sendBatchCallsTransaction(d, e);
		} else {
			await x(n, d, h(t), l);
			let e = await n.getEthereumAddressByChain(d), r = l.map(({ token: e, balance: t }) => ({
				tokenAddress: e.erc20TokenAddress,
				requiredAmount: t
			}));
			await v(n, d, e, h(t), r), m = await t.prooflessDeposit(...o, { value: f > 0n ? f : void 0 });
		}
		w = m.hash;
	}
	return o(p(l, d, r.map(({ erc20TokenAddress: e }) => e), s, y)), w;
}, O = async (e, r, i, a, o, s, c) => {
	let l = a.map(() => i), u = i.erc20TokenAddress;
	return t(r) ? T(e, r, u, s, l, a, o, c) : n[r]?.contractData?.hinkalWrapperAddress ? w(e, r, u, s, l, a, o, c) : await D(e, l, a, o, c, !0);
};
//#endregion
export { D as hinkalProoflessDeposit, O as hinkalProoflessDepositWithPublicFee };
