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