import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import { PAY_SEND_VARIABLE_RATE as n, zeroAddress as r } from "../../constants/protocol.constants.mjs";
import { DepositAndWithdrawPhase as i } from "../../types/scheduled-transactions.types.mjs";
import "../../types/index.mjs";
import { caseInsensitiveEqual as a } from "../../functions/utils/caseInsensitive.utils.mjs";
import { hashEthereumAddress as o, hashString as s } from "../../functions/utils/addresses.mjs";
import { API as c } from "../../API/API.mjs";
import { SWAP_ROUTER_ADDRESSES as l } from "../../constants/addresses.constants.mjs";
import { transactionErrorCodes as u } from "../../error-handling/error-codes.constants.mjs";
import { getERC20Token as d } from "../../functions/utils/erc20tokenFunctions.mjs";
import { getCurrentTimeInSeconds as f } from "../../functions/utils/time.utils.mjs";
import { Utxo as p } from "../utxo/Utxo.mjs";
import { validateAndGetChainId as m } from "../../functions/utils/token-check.utils.mjs";
import { constructAdminData as h } from "../../functions/pre-transaction/constructAdminData.mjs";
import { getFeeStructure as g } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { waitForDepositedUtxosInMerkleTree as _ } from "../../functions/pre-transaction/waitForDepositedUtxosInMerkleTree.mjs";
import { buildCommitmentValidationData as v } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { convertEmporiumOpToCallInfo as y } from "../../functions/utils/convertEmporiumOpToCallInfo.mjs";
import { calculateModifiedFeeStructure as b } from "../../functions/utils/fees.utils.mjs";
import "../../functions/utils/index.mjs";
import { createLifiBridgeOps as x, getAuthorizationDataIfNeeded as S } from "../../functions/private-wallet/emporium.helpers.mjs";
import { constructZkProof as C } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
import { transactCallRelayerBatch as w } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { safeUpdateDepositAndWithdrawStatus as T } from "../../API/deposit-and-withdraw-status-calls.mjs";
import { hinkalDepositOnChainUtxos as E } from "./hinkalDepositOnChainUtxos.mjs";
import "../../functions/index.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalDepositAndBridge.ts
var D = async (n, o, s, d, m, g, _, y, E, D, O) => {
	if (d.length === 0) throw Error("No recipients to bridge");
	let k = s.erc20TokenAddress, { contractData: A } = t[o], { emporiumAddress: j } = A;
	if (!j) throw Error("No Emporium Address");
	let M = l[e.Lifi][o];
	if (!M) throw Error(`LIFI not configured for chain ${o}`);
	let N = await n.getRandomRelay(o);
	if (!N) throw Error(u.RELAYER_NOT_AVAILABLE);
	let P = await n.getEthereumAddressByChain(o), F = f().toString(), I = n.generateProofRemotely ? 5 : 1, L = [];
	for (let t = 0; t < d.length; t += I) {
		let i = d.slice(t, t + I), l = await Promise.all(i.map(async ({ utxo: t, nativeUtxo: i, bridgeAmount: l, quote: u, temporarySubAccount: d, recipientAddress: f }) => {
			await c.addTemporaryWalletNonce(o, g, d.index);
			let _ = u.nativeFee > 0n && !a(k, r), y = [k], w = [-t.amount], T = [!1], E = new p({
				amount: 0n,
				erc20TokenAddress: k,
				nullifyingKey: n.userKeys.getShieldedPrivateKey(),
				timeStamp: F,
				...t.isNewStyle ? {
					spendingPublicKey: n.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,
					isNewStyle: !0
				} : { isNewStyle: !1 }
			}), O = [[t, E]], A = [[E]];
			if (_ && i) {
				let e = new p({
					amount: 0n,
					erc20TokenAddress: r,
					nullifyingKey: n.userKeys.getShieldedPrivateKey(),
					timeStamp: F,
					...i.isNewStyle ? {
						spendingPublicKey: n.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,
						isNewStyle: !0
					} : { isNewStyle: !1 }
				});
				y.push(r), w.push(-i.amount), T.push(!1), O.push([i, e]), A.push([e]);
			}
			let I = x(n, o, d.ethAddress, M, k, t.amount, l, u), L = await b(o, s, l, m), R = h(D, o, y, [-t.amount], P), [{ zkCallData: z, circomData: B, dimData: V }, H, U] = await Promise.all([
				C(n.merkleTreeHinkalByChain[o], O, A, n.userKeys, e.Emporium, j, I, n.generateProofRemotely, L, N ?? r, o, T, void 0, void 0, d.privateKey),
				S(o, d.privateKey),
				v(o, n.userKeys, y, O)
			]);
			return {
				zkCallData: z,
				dimData: V,
				circomData: B,
				commitmentValidationData: U,
				authorizationData: H,
				recipientAddress: f,
				adminData: R
			};
		}));
		L.push(...l);
	}
	await T({
		id: _,
		chainId: o,
		hashedEthereumAddress: g,
		phase: i.BEFORE_SCHEDULE_WITHDRAW
	});
	let R = await w(o, L, g, y, E, O);
	return await T({
		id: _,
		chainId: o,
		hashedEthereumAddress: g,
		phase: i.AFTER_SCHEDULE_WITHDRAW,
		scheduleId: R
	}), R;
}, O = async (t, i, c, u, f, p, h, v, b = !0) => {
	let S = i.erc20TokenAddress, C = m([i]), w = v ? s(v) : void 0, T = o(await t.getEthereumAddressByChain(C)), O = l[e.Lifi][C];
	if (!O) throw Error("LIFI router not configured");
	let k;
	if (f) k = f;
	else {
		let r = c[0], i = x(t, C, r.temporarySubAccount.ethAddress, O, S, r.bridgeAmount, r.bridgeAmount, r.quote).map((e) => y(e, r.temporarySubAccount.ethAddress, C));
		k = await g(C, S, [S], e.Emporium, i, n);
	}
	let A = {
		feeToken: k.feeToken,
		flatFee: 0n,
		variableRate: 0n
	}, j = c.reduce((e, t) => e + t.quote.nativeFee, 0n) > 0n && !a(S, r), M = c.map((e) => e.bridgeAmount), N = c.map((e) => e.recipientAddress), { userDepositedUtxos: P, statusId: F, depositTxHash: I } = await E(t, C, i, M, N, k, T, b), L = [];
	if (j) {
		let e = d(r, C);
		if (!e) throw Error("No native token");
		let { userDepositedUtxos: n } = await E(t, C, e, c.map((e) => e.quote.nativeFee), N, A, T, b);
		L = n;
	}
	return await _(t, C, [...P, ...L]), await D(t, C, i, c.map((e, t) => ({
		...e,
		utxo: P[t].utxo,
		nativeUtxo: j ? L[t]?.utxo : void 0
	})), k, T, F, u, p, h, w), I;
};
//#endregion
export { O as hinkalDepositAndBridge };
