import { CIRCOM_P as e, zeroAddress as t } from "../../constants/protocol.constants.mjs";
import { defaultHookData as n } from "../../types/circom-data.types.mjs";
import { UserKeys as r } from "../../data-structures/crypto-keys/keys.mjs";
import { encryptUtxo as i } from "../../data-structures/crypto-keys/encryptDecryptUtxo.mjs";
import { getExternalActionIdHash as a } from "../utils/external-action.utils.mjs";
import { getOriginalSender as o } from "./getOriginalSender.mjs";
import { ethers as s } from "ethers";
//#region libs/shared/common/src/functions/snarkjs/common.snarkjs.ts
var c = (e) => e.map((e) => e.map((e) => e.amount === 0n ? "0" : e.getCommitment())), l = (t, n, r = !1) => {
	let i = [];
	for (let a = 0; a < t.length; a += 1) {
		let o = 0n, s = 0n;
		for (let e = 0; e < t[a].length; e += 1) o += t[a][e].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(e + (s - o)) : i.push(s - o);
	}
	return i;
}, u = (e) => e.map((e) => e >= 0n ? 0n : e), d = (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(i(e)).toString("hex")}`;
	});
	return t;
}, f = (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;
}, p = (r, i, c, l, u, d, f, p, m, h, g, _, v, y) => {
	let b = Object.values(p || n), x = y ?? o(c ?? t, i), S = s.AbiCoder.defaultAbiCoder().encode([
		"uint16",
		"address",
		"address",
		"uint256",
		"bytes"
	], [
		r,
		i,
		c,
		typeof l == "bigint" ? l : a(l),
		u
	]), C = s.AbiCoder.defaultAbiCoder().encode([
		"tuple(address, address, bytes, bytes)",
		"bytes[][]",
		"tuple(address, uint256, uint256)",
		"int256[]",
		"bool[]",
		"tuple(uint256, uint256,int256[],bool, tuple(int256[] approvalChanges, address[] externalApprovalAddresses, uint256[] conversionInHinkalAddress)[])",
		"tuple(uint8 v, bytes32 r, bytes32 s, uint256 accessKey, uint256 nonce, address ethereumAddress)",
		"address"
	], [
		b,
		d,
		[
			_.feeToken,
			_.flatFee,
			_.variableRate
		],
		m,
		h,
		[
			g.hinkalLogicAction,
			g.inHinkalAddress,
			g.executeApprovalChanges,
			g.doPreTxApproval,
			g.useApprovalUtxoData
		],
		v,
		x
	]), w = BigInt(s.keccak256(S)), T = BigInt(s.keccak256(C)), E = s.AbiCoder.defaultAbiCoder().encode(["uint256", "uint256"], [w, T]);
	return BigInt(s.keccak256(E)) % e;
}, m = (e) => e.startsWith("swapperMW") ? 1 : (e.startsWith("swapperMHA"), 0), h = (e, t, n, r, i, a) => {
	if (e.startsWith("swapperMWMin0")) return 4;
	let o = m(e);
	return 10 + t.length + n.length + r.flat().length + i.flat().length + o + a + 3 * t.length;
}, g = (e, t) => {
	let { h0: n, h1: i } = r.getStealthAddressCompressedPoints(e, t);
	return {
		extraRandomization: e,
		H0: n,
		H1: i,
		stealthAddress: BigInt(r.getStealthAddress(e, t))
	};
};
//#endregion
export { c as buildOutCommitments, l as calcAmountChanges, d as calcEncryptedOutputs, h as calcPublicSignalCount, g as calcStealthAddressStructure, p as createCallDataHash, u as getSlippageValues, f as serializeCircomData };
