import { isNode as e } from "../../constants/vite.constants.mjs";
import { isLocalNetwork as t } from "../../constants/chains.constants.mjs";
import { toBigInt as n } from "../utils/amounts.utils.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 { fetchMerkleTreeSiblings as o } from "./fetchMerkleTreeSiblings.mjs";
import { fetchOnChainRootHashes as s } from "./fetchOnChainRootHashes.mjs";
//#region libs/shared/common/src/functions/snarkjs/getMerkleTreeSiblingsAndRootHashes.ts
var c = async (e, t) => {
	let n = e.getRootHash();
	if (n === void 0) throw Error("Root hash not available from hinkal merkle tree");
	let o;
	return o = await i({
		type: r.ZKProof,
		payload: {
			type: a.CALC_COMMITMENTS_SIBLING_AND_SIDES,
			data: {
				inputUtxosSerialized: t,
				merkleTreeSerialized: e.toJSON()
			}
		}
	}), {
		inCommitmentSiblings: o.inCommitmentSiblings,
		inCommitmentSiblingSides: o.inCommitmentSiblingSides,
		rootHashHinkal: n
	};
}, l = async (e, t) => {
	let r = await o(e, t);
	return {
		inCommitmentSiblings: r.inCommitmentSiblings,
		inCommitmentSiblingSides: r.inCommitmentSiblingSides,
		rootHashHinkal: n(r.rootHashHinkal)
	};
}, u = async (e, t) => {
	if (!t) return !1;
	try {
		let n = t.getRootHash();
		if (n === void 0) return !1;
		let { hinkalRootHash: r } = await s(e);
		return n === r;
	} catch {
		return !1;
	}
}, d = async (e, n, r) => await u(e, r) || t(e) ? c(r, n) : l(e, n), f = async (t, n, o) => {
	if (e) {
		let { inCommitmentSiblings: e, inCommitmentSiblingSides: s, rootHashHinkal: c } = await d(t, o, n);
		return {
			inCommitmentSiblings: e,
			inCommitmentSiblingSides: s,
			rootHashHinkal: c,
			inNullifiers: await i({
				type: r.ZKProof,
				payload: {
					type: a.BUILD_IN_NULLIFIERS,
					data: { inputUtxosSerialized: o }
				}
			})
		};
	} else {
		let [{ inCommitmentSiblings: e, inCommitmentSiblingSides: s, rootHashHinkal: c }, l] = await Promise.all([d(t, o, n), i({
			type: r.ZKProof,
			payload: {
				type: a.BUILD_IN_NULLIFIERS,
				data: { inputUtxosSerialized: o }
			}
		})]);
		return {
			inCommitmentSiblings: e,
			inCommitmentSiblingSides: s,
			rootHashHinkal: c,
			inNullifiers: l
		};
	}
};
//#endregion
export { f as getDataFromWorkers };
