import { isTronLike as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { zeroAddress as n } from "../../constants/protocol.constants.mjs";
import { ContractType as r } from "../../types/ethereum-network.types.mjs";
import { ExternalActionId as i } from "../../types/external-action.types.mjs";
import { AdminTransactionType as a } from "../../types/admin.types.mjs";
import { DepositAndWithdrawPhase as o } from "../../types/scheduled-transactions.types.mjs";
import "../../types/index.mjs";
import { addressToHexFormat as s, evmHexToTronBase58Address as c } from "../../functions/utils/tron.utils.mjs";
import { addPaddingToUtxos as l } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as u } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import { calculateTotalFee as d } from "../../functions/utils/fees.utils.mjs";
import { constructAdminData as f } from "../../functions/pre-transaction/constructAdminData.mjs";
import { getKycAndSignatureData as p } from "../../functions/pre-transaction/getKycAndSignatureData.mjs";
import { getOnChainUtxosFromReceipt as m } from "../../functions/utils/getUtxosFromReceipt.utils.mjs";
import { waitForEthereumTransactionConfirmation as h } from "../../functions/utils/tx-confirmation.utils.mjs";
import { constructZkProof as ee } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { transactCallDirect as g } from "../../functions/web3/functionCalls/transactCallDirect.mjs";
import { emitTxPublicData as _ } from "../../API/admin-calls.mjs";
import { transactCallDirectTron as v } from "../../functions/web3/functionCalls/transactCallDirectTron.mjs";
import { safeUpdateDepositAndWithdrawStatus as y, updateDepositAndWithdrawStatus as b } from "../../API/deposit-and-withdraw-status-calls.mjs";
import { ethers as x } from "ethers";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalDepositOnChainUtxos.ts
var te = async (e, t, n, r) => {
	let i = [...await l(e, t, n, r)], a = [];
	for (let t = 0; t < i.length; t += 1) {
		let { outputUtxos: n } = u(e.userKeys, i[t], r[t]);
		a.push(n);
	}
	return {
		inputUtxosArray: i,
		outputUtxosArray: a
	};
}, S = async (l, u, S, C, w, T, E, D = !0) => {
	let O = e(u), k = S.erc20TokenAddress, A = s(await l.getEthereumAddressByChain(u)), j = C.map((e) => e + d(e, T)), M = j.reduce((e, t) => e + t, 0n), N = [k], P = [0n], F = [!0], I = x.AbiCoder.defaultAbiCoder().encode(["uint256[]"], [j]), { hinkalWrapperAddress: L, hinkalWrapperABI: R, depositOnChainUtxosExternalActionAddress: z, depositOnChainUtxosExternalActionABI: B } = t[u].contractData;
	if (!L || !R || !z || !B) throw Error("contract data not set in networkRegistry");
	let [{ patchAccessTokenMerkleTree: V, signatureData: H }, { inputUtxosArray: U, outputUtxosArray: W }] = await Promise.all([p(l, u, N, P, A), te(l, u, N, P)]), { zkCallData: G, circomData: K, dimData: q } = await ee(l.merkleTreeHinkalByChain[u], l.merkleTreeAccessTokenByChain[u], U, W, l.userKeys, `swapperM1x${U[0].length}x1`, i.DepositOnChainUtxos, z, I, l.generateProofRemotely, n, u, F, void 0, void 0, V, void 0, void 0, l.getContractWithFetcher(u, r.HinkalHelperContract), H, L), J = (await b({
		chainId: u,
		hashedEthereumAddress: E,
		phase: o.BEFORE_DEPOSIT
	})).id ?? void 0, Y;
	if (O) {
		let e = l.getTronWeb();
		Y = await v(l, u, M, S, G, K, q, e.contract(B, c(z)), e.contract(R, c(L)), D);
	} else {
		let e = l.getContractWithSigner(u, r.HinkalWrapper);
		Y = (await g(l, u, M, S, G, K, q, l.getContractWithSigner(u, r.DepositOnChainUtxos), e, D)).hash;
	}
	if (!Y) throw Error("Deposit transaction hash not found");
	let X = await h(u, Y);
	await y({
		id: J,
		chainId: u,
		hashedEthereumAddress: E,
		phase: o.AFTER_DEPOSIT,
		depositTxHash: Y
	});
	let Z = m(X, l, u, k);
	if (Z.length === 0) throw Error("No UTXOs found in deposit transaction. Check contract address and ABI.");
	let Q = [], $ = [...Z];
	return w.forEach((e, t) => {
		let n = j[t], r = $.find((e) => e.amount === n);
		if (!r) throw Error(`Could not find newly created UTXO with amount ${n} for recipient ${e}.`);
		Q.push({
			recipientAddress: e,
			utxo: r
		});
		let i = $.indexOf(r);
		$.splice(i, 1);
	}), _(f(a.DepositOnChainUtxos, u, N, [M], A)), {
		userDepositedUtxos: Q,
		statusId: J,
		depositTxHash: Y
	};
};
//#endregion
export { S as hinkalDepositOnChainUtxos };
