import { toBigInt as e } from "../../../functions/utils/amounts.utils.mjs";
import { poseidonFunction as t } from "../../../crypto/poseidon.mjs";
import "../../../crypto/index.mjs";
import { MerkleTree as n } from "../../merkle-tree/MerkleTree.mjs";
import "../../merkle-tree/index.mjs";
import { AbstractSnapshotService as r } from "./AbstractSnapshotService.mjs";
import { contructMerkleTreeFromSerialized as i } from "../../../functions/utils/merkleTree.utils.mjs";
//#region libs/shared/common/src/data-structures/event-service/evm/AbstractCommitmentsSnapshotService.ts
var a = class extends r {
	constructor(e) {
		super(e, "NewCommitment");
	}
	_merkleTree;
	_encryptedOutputs;
	_encryptedOutputsByCommitment = /* @__PURE__ */ new Map();
	get merkleTree() {
		return this._merkleTree;
	}
	get encryptedOutputs() {
		return this._encryptedOutputs;
	}
	serializeSnapshot(e) {
		return {
			merkleTree: e.payload.merkleTree.toJSON(),
			latestBlockNumber: e.latestBlockNumber,
			encryptedOutputs: e.payload.encryptedOutputs.map((e) => ({
				value: e.value,
				isPositive: String(e.isPositive),
				isBlocked: e.isBlocked ?? !1
			})),
			encryptedOutputsByCommitment: Array.from(e.payload.encryptedOutputsByCommitment.entries()).map(([e, t]) => ({
				commitment: e.toString(),
				encryptedOutput: t
			}))
		};
	}
	deserializeSnapshot(r) {
		let a;
		a = r.latestBlockNumber && r.merkleTree ? i(r.merkleTree) : n.create(t, 0n);
		let o = r.encryptedOutputs?.map((e) => ({
			value: e.value,
			isPositive: JSON.parse(e.isPositive),
			isBlocked: e.isBlocked ?? !1
		})), s = /* @__PURE__ */ new Map();
		return r.encryptedOutputsByCommitment?.forEach(({ commitment: t, encryptedOutput: n }) => {
			s.set(e(t), n);
		}), {
			latestBlockNumber: r.latestBlockNumber ?? 0,
			payload: {
				merkleTree: a,
				encryptedOutputs: o || [],
				encryptedOutputsByCommitment: s
			}
		};
	}
	getSnapshotPayload() {
		return {
			merkleTree: this._merkleTree,
			encryptedOutputs: this._encryptedOutputs,
			encryptedOutputsByCommitment: this._encryptedOutputsByCommitment
		};
	}
	populateSnapshot({ payload: { merkleTree: e, encryptedOutputs: t, encryptedOutputsByCommitment: n } }) {
		this._merkleTree = e, this._encryptedOutputs = t, this._encryptedOutputsByCommitment = n;
	}
	mapEvent(t) {
		let n = t.getArg("commitment"), r = t.getArg("index"), i = t.getArg("encryptedOutput");
		return {
			commitment: e(n),
			index: e(r),
			encryptedOutput: i
		};
	}
	acceptEvent(e, t, n, r) {
		let i = e.index >= 0n ? e.index : -1n * e.index, a = this._merkleTree.getValue(i);
		if (a === e.commitment) return !1;
		this._merkleTree.insert(e.commitment, i);
		let o = a === void 0 ? void 0 : this._encryptedOutputsByCommitment.get(a);
		if (a !== void 0 && o !== void 0) {
			let e = this.encryptedOutputs.findIndex((e) => e.value === o);
			e !== -1 && this.encryptedOutputs.splice(e, 1), this._encryptedOutputsByCommitment.delete(a);
		}
		if (this._encryptedOutputsByCommitment.set(e.commitment, e.encryptedOutput), this.encryptedOutputs.map((e) => e.value).includes(e.encryptedOutput)) return !0;
		let s = {
			value: e.encryptedOutput,
			isPositive: e.index >= 0n,
			isBlocked: r ?? !1
		};
		return this._encryptedOutputs.push(s), !0;
	}
};
//#endregion
export { a as AbstractCommitmentsSnapshotService };
