import { isTronLike as e } from "../../constants/chains.constants.mjs";
import { zeroAddress as t } from "../../constants/protocol.constants.mjs";
import { ContractType as n } from "../../types/ethereum-network.types.mjs";
import { ExternalActionId as r } from "../../types/external-action.types.mjs";
import { DepositAndWithdrawPhase as i } from "../../types/scheduled-transactions.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as a } from "../../error-handling/error-codes.constants.mjs";
import { caseInsensitiveEqual as o } from "../../functions/utils/caseInsensitive.utils.mjs";
import { getCurrentTimeInSeconds as s } from "../../functions/utils/time.utils.mjs";
import { Utxo as c } from "../utxo/Utxo.mjs";
import { hashEthereumAddress as l, hashString as u } from "../../functions/utils/addresses.mjs";
import { addressToHexFormat as d, reorderZkCallData as f } from "../../functions/utils/tron.utils.mjs";
import { validateAndGetChainId as p } from "../../functions/utils/token-check.utils.mjs";
import { calculateTotalFee as m } from "../../functions/utils/fees.utils.mjs";
import { constructAdminData as h } from "../../functions/pre-transaction/constructAdminData.mjs";
import { getFeeStructure as g } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { getKycAndSignatureData as _ } from "../../functions/pre-transaction/getKycAndSignatureData.mjs";
import { waitForDepositedUtxosInMerkleTree as v } from "../../functions/pre-transaction/waitForDepositedUtxosInMerkleTree.mjs";
import { buildCommitmentValidationData as y } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { constructZkProof as b } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { transactCallRelayerBatch as x } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { safeUpdateDepositAndWithdrawStatus as S } from "../../API/deposit-and-withdraw-status-calls.mjs";
import { hinkalDepositOnChainUtxos as C } from "./hinkalDepositOnChainUtxos.mjs";
import "../../functions/index.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalDepositAndWithdraw.ts
var w = async (r, l, u, p, g, v, C, w, T, E, D, O) => {
	let k = e(l);
	if (p.length === 0) throw Error("userDepositedUtxos must not be empty");
	let A = u.erc20TokenAddress, [j, M] = await Promise.all([r.getRandomRelay(l), r.getEthereumAddressByChain(l)]);
	if (!j) throw Error(a.RELAYER_NOT_AVAILABLE);
	let N = s().toString(), P = r.generateProofRemotely && !k ? 5 : 1, F = [], I = d(M);
	for (let e = 0; e < p.length; e += P) {
		let i = p.slice(e, e + P), a = await Promise.all(i.map(async ({ recipientAddress: i, utxo: a }) => {
			let s = d(i), u = [-a.amount], p = new c({
				amount: 0n,
				erc20TokenAddress: A,
				shieldedPrivateKey: r.userKeys.getShieldedPrivateKey(),
				timeStamp: N,
				tokenId: 0
			}), x = [[o(a.erc20TokenAddress, A) ? a : c.createFrom(a, { erc20TokenAddress: A }), p]], S = [[p]], C = `swapperM1x${x[0].length}x1`, { patchAccessTokenMerkleTree: w, signatureData: T } = await _(r, l, [A], u, I), E = {
				externalActionId: 0n,
				externalAddress: s,
				externalActionMetadata: "0x00"
			}, O = h(D, l, [A], [-a.amount], I), M = m(g[e], v), P = {
				feeToken: v.feeToken,
				flatFee: M,
				variableRate: 0n
			}, [{ zkCallData: F, circomData: L, dimData: R }, z] = await Promise.all([b(r.merkleTreeHinkalByChain[l], r.merkleTreeAccessTokenByChain[l], x, S, r.userKeys, C, E.externalActionId, E.externalAddress, E.externalActionMetadata, r.generateProofRemotely, j ?? t, l, void 0, void 0, void 0, w, void 0, P, r.getContractWithFetcher(l, n.HinkalHelperContract), T), y(l, r.userKeys, [A], x)]);
			return {
				zkCallData: F,
				dimData: R,
				circomData: L,
				commitmentValidationData: z,
				adminData: O,
				recipientAddress: i,
				tronProofSignature: await f(k, F, R, L)
			};
		}));
		F.push(...a);
	}
	await S({
		id: w,
		chainId: l,
		hashedEthereumAddress: C,
		phase: i.BEFORE_SCHEDULE_WITHDRAW
	});
	let L = await x(l, F, C, T, E, O);
	return await S({
		id: w,
		chainId: l,
		hashedEthereumAddress: C,
		phase: i.AFTER_SCHEDULE_WITHDRAW,
		scheduleId: L
	}), L;
}, T = async (e, t, n, i, a, o, s, c, f, m = !0) => {
	let h = p([t]), _ = f ? u(f) : void 0, y = t.erc20TokenAddress, b = l(d(await e.getEthereumAddressByChain(h))), x = o ?? await g(h, y, [y], r.Transact, [], 10n), { userDepositedUtxos: S, statusId: T, depositTxHash: E } = await C(e, h, t, n, i, x, b, m);
	return await v(e, h, S), await w(e, h, t, S, n, x, b, T, a, s, c, _), E;
};
//#endregion
export { T as hinkalDepositAndWithdraw };
