import { defaultHookData as e } from "../../types/circom-data.types.mjs";
import { CIRCOM_P as t, zeroAddress as n } from "../../constants/protocol.constants.mjs";
import { UserKeys as r } from "../../data-structures/crypto-keys/keys.mjs";
import { addHighestBitToNumber as i } from "../utils/bit.operations.mjs";
import { encryptUtxo as a } from "../../data-structures/crypto-keys/encryptDecryptUtxo.mjs";
import { getExternalActionIdHash as o } from "../utils/external-action.utils.mjs";
import { getOriginalSender as s } from "./getOriginalSender.mjs";
import { ethers as c } from "ethers";
//#region libs/shared/common/src/functions/snarkjs/common.snarkjs.ts
var l = (e) => {
	if (e.H0?.[0] !== void 0 && e.H0[1] !== void 0) return [e.H0[0], e.H0[1]];
	if (e.randomization !== void 0) return r.getH0FromRandomization(e.randomization);
	throw Error("UTXO missing H0 coordinates for proof input");
}, u = (e) => {
	if (e.randomization !== void 0) return e.randomization.toString();
	if (!e.isNewStyle) throw Error("UTXO missing randomization for old-style proof input");
	return "0";
}, d = (e, t) => e.map((e, n) => e.map((e) => t[n] ? "0" : e.getNullifier())), f = (e) => e.map((e) => e.map((e) => e.amount === 0n ? "0" : e.getCommitment())), p = (e, n, r = !1) => {
	let i = [];
	for (let a = 0; a < e.length; a += 1) {
		let o = 0n, s = 0n;
		for (let t = 0; t < e[a].length; t += 1) o += e[a][t].amount;
		for (let e = 0; e < n[a].length; e += 1) s += n[a][e].amount;
		s - o < 0 ? r ? i.push(s - o) : i.push(t + (s - o)) : i.push(s - o);
	}
	return i;
}, m = (e) => e.map((e) => e >= 0n ? 0n : e), h = (e) => {
	if (e.length === 0) return [];
	let t = [[""]];
	for (let n = 0; n < e.length; n += 1) n > 0 && t.push([""]), e[n].forEach((e, r) => {
		t[n][r] = `0x${Buffer.from(a(e)).toString("hex")}`;
	});
	return t;
}, g = (e, t) => {
	let n = [];
	for (let r = 0; r < e.length; r += 1) {
		n.push([]);
		for (let i = 0; i < e[r].length; i += 1) n[r][i] = t.getSiblingHashesForVerification(BigInt(e[r][i].getCommitment())).map((e) => e.toString());
	}
	let r = [];
	for (let n = 0; n < e.length; n += 1) {
		r.push([]);
		for (let i = 0; i < e[n].length; i += 1) r[n][i] = t.getSiblingSides(BigInt(e[n][i].getCommitment())).map((e) => e.toString());
	}
	return {
		inCommitmentSiblings: n,
		inCommitmentSiblingSides: r
	};
}, _ = (e) => {
	let t = { ...e };
	return t.amountChanges = e.amountChanges.map((e) => e.toString()), t.calldataHash = e.calldataHash.toString(), t.rootHashAccessToken = e.rootHashAccessToken?.toString(), t.stealthAddressStructure.extraRandomization = e.stealthAddressStructure.extraRandomization.toString(), t.stealthAddressStructure.stealthAddress = e.stealthAddressStructure.stealthAddress.toString(), t.stealthAddressStructure.H0 = e.stealthAddressStructure.H0.toString(), t.stealthAddressStructure.H1 = e.stealthAddressStructure.H1.toString(), t.rootHashHinkal = e.rootHashHinkal?.toString(), t.externalActionId = e.externalActionId?.toString(), t.slippageValues = e.slippageValues.map((e) => e.toString()), t.hinkalLogicArgs.inHinkalAddress = e.hinkalLogicArgs.inHinkalAddress.toString(), t.hinkalLogicArgs.executeApprovalChanges = e.hinkalLogicArgs.executeApprovalChanges.map((e) => e.toString()), t.hinkalLogicArgs.useApprovalUtxoData = e.hinkalLogicArgs.useApprovalUtxoData.map((e) => ({
		approvalChanges: e.approvalChanges.map((e) => e.toString()),
		externalApprovalAddresses: e.externalApprovalAddresses,
		conversionInHinkalAddress: e.conversionInHinkalAddress.map((e) => e.toString())
	})), t.feeStructure.flatFee = e.feeStructure.flatFee.toString(), t.feeStructure.variableRate = e.feeStructure.variableRate.toString(), t;
}, v = (e) => {
	let t = {
		...e.hinkalLogicArgs,
		inHinkalAddress: BigInt(e.hinkalLogicArgs.inHinkalAddress),
		executeApprovalChanges: e.hinkalLogicArgs.executeApprovalChanges.map((e) => BigInt(e)),
		useApprovalUtxoData: e.hinkalLogicArgs.useApprovalUtxoData.map((e) => ({
			...e,
			approvalChanges: e.approvalChanges.map((e) => BigInt(e)),
			conversionInHinkalAddress: e.conversionInHinkalAddress.map((e) => BigInt(e))
		}))
	};
	return {
		...e,
		amountChanges: e.amountChanges.map((e) => BigInt(e)),
		calldataHash: BigInt(e.calldataHash),
		rootHashAccessToken: BigInt(e.rootHashAccessToken),
		rootHashHinkal: BigInt(e.rootHashHinkal),
		externalActionId: BigInt(e.externalActionId),
		stealthAddressStructure: {
			extraRandomization: BigInt(e.stealthAddressStructure.extraRandomization),
			stealthAddress: BigInt(e.stealthAddressStructure.stealthAddress),
			H0: BigInt(e.stealthAddressStructure.H0),
			H1: BigInt(e.stealthAddressStructure.H1)
		},
		feeStructure: {
			...e.feeStructure,
			flatFee: BigInt(e.feeStructure.flatFee),
			variableRate: BigInt(e.feeStructure.variableRate)
		},
		slippageValues: e.slippageValues.map((e) => BigInt(e)),
		hinkalLogicArgs: t
	};
}, y = (r, i, a, l, u, d, f, p, m, h, g, _) => {
	let v = Object.values(f || e), y = _ ?? s(a ?? n, i), b = c.AbiCoder.defaultAbiCoder().encode([
		"uint16",
		"address",
		"address",
		"uint256",
		"bytes"
	], [
		r,
		i,
		a,
		typeof l == "bigint" ? l : o(l),
		u
	]), x = c.AbiCoder.defaultAbiCoder().encode([
		"tuple(address, address, bytes, bytes)",
		"bytes[][]",
		"tuple(address, uint256, uint256)",
		"int256[]",
		"bool[]",
		"tuple(uint8 v, bytes32 r, bytes32 s, uint256 accessKey, uint256 nonce, address ethereumAddress)",
		"address"
	], [
		v,
		d,
		[
			h.feeToken,
			h.flatFee,
			h.variableRate
		],
		p,
		m,
		g,
		y
	]), S = BigInt(c.keccak256(b)), C = BigInt(c.keccak256(x)), w = c.AbiCoder.defaultAbiCoder().encode(["uint256", "uint256"], [S, C]);
	return BigInt(c.keccak256(w)) % t;
}, b = (e, t, n, r, i) => e.startsWith("mainEVMCircuitMin0") ? 3 : 2 + t.length + n.length + 1 + r.flat().length + i.flat().length + 1 + 1 + 1 + 1 + 1 + 1, x = (e, t, n) => {
	let { H0: a } = r.getRandomizedStealthPair(e, t), o = r.getH1FromH0(a, t), s = BigInt(r.getStealthAddressNewStyle(a, t, n));
	return {
		extraRandomization: i(a[0]),
		H0: a[1],
		H1: o[1],
		stealthAddress: s
	};
}, S = (e, t) => t.length === 0 ? "mainEVMCircuitMin0" : `mainEVMCircuit${e.length}x${e[0].length}x${t[0].length}`;
//#endregion
export { d as buildInNullifiers, f as buildOutCommitments, p as calcAmountChanges, g as calcCommitmentsSiblingAndSides, h as calcEncryptedOutputs, b as calcPublicSignalCount, x as calcStealthAddressStructure, y as createCallDataHash, v as deserializeCircomData, m as getSlippageValues, l as getUtxoCircuitH0Coords, u as getUtxoCircuitInRandomization, S as getZkProofVerifierName, _ as serializeCircomData };
