import { ExternalActionId as e } from "../../types/external-action.types.mjs";
import { chainIds as t, getOnchainChainId as n } from "../../constants/chains.constants.mjs";
import { defaultSignatureData as r, zeroAddress as i } from "../../constants/protocol.constants.mjs";
import { randomBigInt as a } from "../web3/etherFunctions.mjs";
import { poseidonFunction as ee } from "../../crypto/poseidon.mjs";
import { UserKeys as te } from "../../data-structures/crypto-keys/keys.mjs";
import { getCurrentTimeInSeconds as ne } from "../utils/time.utils.mjs";
import { takeOffHighestBit as re } from "../utils/bit.operations.mjs";
import { Logger as ie } from "../../error-handling/logger.mjs";
import { buildOutCommitments as ae, calcAmountChanges as o, calcEncryptedOutputs as s, calcPublicSignalCount as c, calcStealthAddressStructure as l, createCallDataHash as u, getSlippageValues as d, getUtxoCircuitH0Coords as f, getUtxoCircuitInRandomization as oe, getZkProofVerifierName as se } from "./common.snarkjs.mjs";
import { computeSignedMessageHashEvm as ce } from "./signedMessageHash.mjs";
import { generateCircomData as le } from "./generateCircomData.mjs";
import { generateZkProof as ue } from "./generateZkProof.mjs";
import { AccountActions as de } from "../../data-structures/AccountActions/AccountActions.mjs";
import { getDataFromWorkers as p } from "./getMerkleTreeSiblingsAndRootHashes.mjs";
import { encodeEmporiumMetadata as m } from "../private-wallet/emporium.helpers.mjs";
//#region libs/shared/common/src/functions/snarkjs/constructGeneralZkProof.ts
var h = async (h, g, _, v, y, b, x, fe, S, C = i, w = t.localhost, T = g.map(() => !1), E = void 0, D, O) => {
	let k = se(g, _), A = o(g, _), j = g.map((e) => e[0].erc20TokenAddress), M = s(_), N = r, P = v.getShieldedPrivateKey(), { pubSpendingBJJPoint: F } = v.getSpendingKeyPair(), I = l(te.findCorrectRandomization(a(31), P), P, v.getSpendingKeyPair().pubSpendingBJJPoint), { inCommitmentSiblings: L, inCommitmentSiblingSides: R, rootHashHinkal: z, inNullifiers: B } = await p(w, h, g.map((e) => e.map((e) => e.getConstructableParams()))), V = ae(_), H = a(31), U = ee(H), W = BigInt(_.length > 0 ? _[0][0].timeStamp : ne()), G = {
		rootHashHinkal: z,
		spendingPublicKey: F,
		nullifyingPrivateKey: P,
		erc20TokenAddresses: j,
		amountChanges: A,
		inAmounts: g.map((e) => e.map((e) => e.amount.toString())),
		inRandomizations: g.map((e) => e.map((e) => oe(e))),
		inH0Ax: g.map((e) => e.map((e) => f(e)[0].toString())),
		inH0Ay: g.map((e) => e.map((e) => f(e)[1].toString())),
		isNewStyle: g.map((e) => e.map((e) => !!e.isNewStyle)),
		inTimeStamps: g.map((e) => e.map((e) => e.timeStamp)),
		inNullifiers: B,
		inCommitmentSiblings: L,
		inCommitmentSiblingSides: R,
		outAmounts: _.map((e) => e.map((e) => e.amount.toString())),
		outTimeStamp: W,
		outPublicKeys: _.map((e) => e.map((e) => e.getStealthAddress())),
		outCommitments: V,
		calldataHash: 0n,
		messageSeed: H,
		H0Ax: re(I.extraRandomization),
		H0Ay: I.H0
	}, K = c(k, G.erc20TokenAddresses, G.amountChanges, G.inNullifiers, G.outCommitments), q = o(g, _, !0), J = d(q);
	ie.log({ publicSignalCount: K });
	let Y = Array.isArray(x) ? x : [x], pe = n(w), me = O ? de.getSignerAddressFromPrivateKey(pe, O) : void 0, X = y === e.Emporium ? await m(w, b ?? i, O, Y, U, me) : await m(w, i, void 0, y === 0n ? [] : Y, U, i);
	G.calldataHash = u(K, C, b, y, X, M, E, J, T, S, N, D);
	let Z = ce({
		rootHashHinkal: z,
		erc20TokenAddresses: j,
		amountChanges: A,
		outTimeStamp: W,
		inNullifiers: B,
		outCommitments: V,
		calldataHash: G.calldataHash,
		message: U,
		outH1Ay: I.H1,
		H0Ax: G.H0Ax,
		H0Ay: I.H0
	}), { R8: Q, S: he } = v.signEddsa(Z);
	G.eddsaSignature = [
		Q[0],
		Q[1],
		he
	], G.signedMessageHash = Z;
	let $ = k.startsWith("mainEVMCircuitMin0"), ge = {
		outTimeStamp: W,
		calldataHash: G.calldataHash,
		messageSeed: H
	}, { zkCallData: _e } = await ue(w, k, $ ? ge : G, fe);
	return {
		zkCallData: _e,
		circomData: le(V, B, z, q, j, _, M, K, y, b, X, C, G.calldataHash, I, T, E, $ ? Number(W) : void 0, J, S, N, D),
		dimData: {
			tokenNumber: g.length,
			nullifierAmount: g.length > 0 ? g[0].length : 0,
			outputAmount: _.length > 0 ? _[0].length : 0
		},
		encryptedOutputs: M
	};
};
//#endregion
export { h as constructZkProof };
