import { isNode as e } from "../../constants/vite.constants.mjs";
import { toBigInt as t } from "../utils/amounts.utils.mjs";
import { poseidonFunction as n } from "../../crypto/poseidon.mjs";
import { WorkerVariant as r } from "../../webworker/worker.registry.mjs";
import { performTaskWithWorker as i } from "../../webworker/performTaskWithWorker.mjs";
import { ZKProofWorkerActionType as a } from "../../webworker/zkProofWorker/zkProofWorker.types.mjs";
import { MerkleTree as o } from "../../data-structures/merkle-tree/MerkleTree.mjs";
import { getPatchedAccessTokenMerkleTree as s } from "../../data-structures/merkle-tree/getPatchedAccessTokenMerkleTree.mjs";
import "../../data-structures/merkle-tree/index.mjs";
import { fetchMerkleTreeSiblings as c } from "./fetchMerkleTreeSiblings.mjs";
import { fetchOnChainRootHashes as l } from "./fetchOnChainRootHashes.mjs";
//#region libs/shared/common/src/functions/snarkjs/getMerkleTreeSiblingsAndRootHashes.ts
var u = async (e, t) => {
	let n = e.getRootHash();
	if (n === void 0) throw Error("Root hash not available from patched merkle tree");
	let o = await i({
		type: r.ZKProof,
		payload: {
			type: a.CALC_ACCESS_TOKEN_SIBLING_AND_SIDES,
			data: {
				accessKey: t,
				merkleTreeAccessTokenSerialized: e.toJSON()
			}
		}
	});
	return {
		accessTokenSiblings: o.accessTokenSiblings,
		accessTokenSiblingSides: o.accessTokenSiblingSides,
		rootHashAccessToken: n
	};
}, d = async (t, n, o, c, l, d) => {
	let f = o.getAccessKey(), p = s(n, f, l, d), m = t.getRootHash();
	if (m === void 0) throw Error("Root hash not available from hinkal merkle tree");
	let h, g;
	return e ? (h = await u(p, f), g = await i({
		type: r.ZKProof,
		payload: {
			type: a.CALC_COMMITMENTS_SIBLING_AND_SIDES,
			data: {
				inputUtxosSerialized: c,
				merkleTreeSerialized: t.toJSON()
			}
		}
	})) : [h, g] = await Promise.all([u(p, f), i({
		type: r.ZKProof,
		payload: {
			type: a.CALC_COMMITMENTS_SIBLING_AND_SIDES,
			data: {
				inputUtxosSerialized: c,
				merkleTreeSerialized: t.toJSON()
			}
		}
	})]), {
		inCommitmentSiblings: g.inCommitmentSiblings,
		inCommitmentSiblingSides: g.inCommitmentSiblingSides,
		accessTokenSiblings: h.accessTokenSiblings,
		accessTokenSiblingSides: h.accessTokenSiblingSides,
		rootHashHinkal: m,
		rootHashAccessToken: h.rootHashAccessToken
	};
}, f = async (e, r, i, a, l) => {
	let d = r.getAccessKey();
	if (!l || a) {
		let r = s(o.create(n, 0n), d, a, l), [f, p] = await Promise.all([u(r, d), c(e, i, d)]);
		return {
			inCommitmentSiblings: p.inCommitmentSiblings,
			inCommitmentSiblingSides: p.inCommitmentSiblingSides,
			accessTokenSiblings: f.accessTokenSiblings,
			accessTokenSiblingSides: f.accessTokenSiblingSides,
			rootHashHinkal: t(p.rootHashHinkal),
			rootHashAccessToken: f.rootHashAccessToken
		};
	}
	let f = await c(e, i, d);
	return {
		inCommitmentSiblings: f.inCommitmentSiblings,
		inCommitmentSiblingSides: f.inCommitmentSiblingSides,
		accessTokenSiblings: f.accessTokenSiblings,
		accessTokenSiblingSides: f.accessTokenSiblingSides,
		rootHashHinkal: t(f.rootHashHinkal),
		rootHashAccessToken: t(f.rootHashAccessToken)
	};
}, p = async (e, t, n) => {
	if (!t || !n) return !1;
	try {
		let r = t.getRootHash(), i = n.getRootHash();
		if (r === void 0 || i === void 0) return !1;
		let { hinkalRootHash: a, accessTokenRootHash: o } = await l(e);
		return r === a && i === o;
	} catch {
		return !1;
	}
}, m = async (e, t, n, r, i, a, o) => await p(e, a, o) ? d(a, o, t, n, r, i) : f(e, t, n, r, i), h = async (t, n, o, s, c, l, u) => {
	if (e) {
		let { inCommitmentSiblings: e, inCommitmentSiblingSides: d, accessTokenSiblings: f, accessTokenSiblingSides: p, rootHashHinkal: h, rootHashAccessToken: g } = await m(t, s, c, l, u, n, o);
		return {
			inCommitmentSiblings: e,
			inCommitmentSiblingSides: d,
			accessTokenSiblings: f,
			accessTokenSiblingSides: p,
			rootHashHinkal: h,
			rootHashAccessToken: g,
			inNullifiers: await i({
				type: r.ZKProof,
				payload: {
					type: a.BUILD_IN_NULLIFIERS,
					data: { inputUtxosSerialized: c }
				}
			})
		};
	} else {
		let [{ inCommitmentSiblings: e, inCommitmentSiblingSides: d, accessTokenSiblings: f, accessTokenSiblingSides: p, rootHashHinkal: h, rootHashAccessToken: g }, _] = await Promise.all([m(t, s, c, l, u, n, o), i({
			type: r.ZKProof,
			payload: {
				type: a.BUILD_IN_NULLIFIERS,
				data: { inputUtxosSerialized: c }
			}
		})]);
		return {
			inCommitmentSiblings: e,
			inCommitmentSiblingSides: d,
			accessTokenSiblings: f,
			accessTokenSiblingSides: p,
			rootHashHinkal: h,
			rootHashAccessToken: g,
			inNullifiers: _
		};
	}
};
//#endregion
export { h as getDataFromWorkers };
