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