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