import { ContractType as e } from "../../types/ethereum-network.types.mjs";
import { isSolanaLike as t, networkRegistry as n } from "../../constants/chains.constants.mjs";
import { PrivateBalanceUpdateType as r } from "../../types/balances.types.mjs";
import { EventCategory as i } from "../../types/events.types.mjs";
import "../../types/index.mjs";
import { getChainBalanceFetchingMutex as a } from "../../functions/utils/mutexes.utils.mjs";
import { createCustomSolanaConnection as o } from "../../functions/utils/create-provider.mjs";
import { getContractAddress as s } from "../../functions/utils/getContractAddress.mjs";
import { balanceChangedHandler as c } from "../../functions/web3/events/balanceChangedHandler.mjs";
import { ClientBlockchainEventEmitter as l } from "../event-service/evm/ClientBlockchainEventEmitter.mjs";
import { ClientSolanaBlockchainEventEmitter as u } from "../event-service/solana/ClientSolanaBlockchainEventEmitter.mjs";
import "../event-service/index.mjs";
import { SnapshotFetcherService as d } from "../snapshot/SnapshotFetcherService.mjs";
import { ClientSolanaCommitmentsSnapshotService as f } from "../snapshot/solana-snapshots/ClientSolanaCommitmentsSnapshotService.mjs";
import { ClientSolanaNullifierSnapshotService as p } from "../snapshot/solana-snapshots/ClientSolanaNullifierSnapshotService.mjs";
import { ClientCommitmentsSnapshotService as m } from "../snapshot/evm-snapshots/ClientCommitmentsSnapshotService.mjs";
import { ClientNullifierSnapshotService as h } from "../snapshot/evm-snapshots/ClientNullifierSnapshotService.mjs";
import "../snapshot/index.mjs";
import { Mutex as g } from "async-mutex";
import { PublicKey as _ } from "@solana/web3.js";
//#region libs/shared/common/src/data-structures/Hinkal/resetMerkleTrees.ts
var v = /* @__PURE__ */ new Map(), y = (e) => {
	let t = v.get(e);
	return t || (t = new g(), v.set(e, t)), t;
}, b = async (g, v) => {
	let b = (v ?? g.getSupportedChains()).map((v) => y(v).runExclusive(async () => {
		g.commitmentsSnapshotServiceByChain[v]?.intervalClear(), g.nullifierSnapshotServiceByChain[v]?.intervalClear();
		let { maxPageSize: y, fetchRpcUrl: b, contractData: x } = n[v], S;
		if (t(v)) {
			let e = x.hinkalAddress;
			if (!b || !e) throw Error("Fetch RPC URL or Solana program ID is not set");
			S = new u(v, o(b), new _(e), 0, a(v), y);
			let t = x.hinkalIdl;
			t && S.setIdl(t);
			let n = new d(v, e);
			g.commitmentsSnapshotServiceByChain[v] = new f(S, n), g.nullifierSnapshotServiceByChain[v] = new p(S, n);
		} else {
			let t = g.getContractWithFetcherByChainId(v, e.HinkalContract), n = g.getContractWithFetcherByChainId(v, e.DepositOnChainUtxos), r = a(v);
			S = new l(i.MainContractEvents, v, t, 0, r, y, n);
			let o = new d(v, s(t));
			g.commitmentsSnapshotServiceByChain[v] = new m(S, o), g.nullifierSnapshotServiceByChain[v] = new h(S, o);
		}
		let C = g.commitmentsSnapshotServiceByChain[v], w = g.nullifierSnapshotServiceByChain[v];
		if (!C || !w) throw Error(`Missing snapshot service for chain ${v}`);
		await Promise.all([C.init(), w.init()]), await S.init(), g.merkleTreeHinkalByChain[v] = C.merkleTree, g.nullifiersByChain[v] = w.nullifiers, g.encryptedOutputsByChain[v] = C.encryptedOutputs, S.onEventsProcessed = (e) => {
			e > 0 && c({
				updateType: r.FreshWithoutLoader,
				updateTokensListBefore: !0,
				chainIdToUpdate: v
			});
		};
	}));
	await Promise.allSettled(b);
};
//#endregion
export { b as resetMerkleTrees };
