import { zeroAddress as e } from "../../../constants/protocol.constants.mjs";
import { toBigInt as t } from "../../../functions/utils/amounts.utils.mjs";
import { poseidonFunction as n } from "../../../crypto/poseidon.mjs";
import { MerkleTree as r } from "../../merkle-tree/MerkleTree.mjs";
import "../../merkle-tree/index.mjs";
import "../../../crypto/index.mjs";
import { AbstractSnapshotService as i } from "./AbstractSnapshotService.mjs";
import { contructMerkleTreeFromSerialized as a } from "../../../functions/utils/merkleTree.utils.mjs";
//#region libs/shared/common/src/data-structures/event-service/evm/AbstractAccessTokenSnapshotService.ts
var o = class extends i {
	constructor(e) {
		super(e, "NewAccessKeyAdded");
	}
	_merkleTree;
	_senderAddresses;
	_senderAddressIndexMap;
	get merkleTree() {
		return this._merkleTree;
	}
	get senderAddresses() {
		return this._senderAddresses;
	}
	get senderAddressIndexMap() {
		return this._senderAddressIndexMap;
	}
	serializeSnapshot(e) {
		return {
			merkleTree: e.payload.merkleTree.toJSON(),
			senderAddresses: Array.from(e.payload.senderAddresses),
			latestBlockNumber: e.latestBlockNumber,
			senderAddressIndexMap: Array.from(e.payload.senderAddressIndexMap).map(([e, t]) => ({
				address: e,
				index: t
			}))
		};
	}
	deserializeSnapshot(e) {
		let t, i;
		return t = e.latestBlockNumber && e.merkleTree ? a(e.merkleTree) : r.create(n, 0n), i = e.latestBlockNumber && e.senderAddressIndexMap ? new Map(e.senderAddressIndexMap.map(({ address: e, index: t }) => [e, t])) : /* @__PURE__ */ new Map(), {
			latestBlockNumber: e.latestBlockNumber ?? 0,
			payload: {
				merkleTree: t,
				senderAddresses: e.senderAddresses ?? [],
				senderAddressIndexMap: i
			}
		};
	}
	acceptEvent(t, n) {
		try {
			return this._merkleTree.insert(t.accessKey, t.index), this._senderAddressIndexMap.set(t.senderAddress, Math.max(Number(t.index), this._senderAddressIndexMap.get(t.senderAddress) ?? 0)), this._senderAddresses.includes(t.senderAddress) || this._senderAddresses.push(t.senderAddress), t.accessKey === 0n && t.senderAddress === e && this._merkleTree.remove(t.index), !0;
		} catch (e) {
			return console.error(e), !1;
		}
	}
	mapEvent(e) {
		let n = e.getArg("accessKey"), r = e.getArg("index"), i = e.getArg("senderAddress");
		return {
			accessKey: t(n),
			index: t(r),
			senderAddress: i
		};
	}
	getSnapshotPayload() {
		return {
			merkleTree: this._merkleTree,
			senderAddresses: this._senderAddresses,
			senderAddressIndexMap: this._senderAddressIndexMap
		};
	}
	populateSnapshot({ payload: { merkleTree: e, senderAddresses: t, senderAddressIndexMap: n } }) {
		this._merkleTree = e, this._senderAddresses = t, this._senderAddressIndexMap = n;
	}
};
//#endregion
export { o as AbstractAccessTokenSnapshotService };
