import { caseInsensitiveEqual as e } from "../../functions/utils/caseInsensitive.utils.mjs";
import { fetchSnapshots as t } from "../../API/fetchCommitmentsCache.mjs";
//#region libs/shared/common/src/data-structures/snapshot/SnapshotFetcherService.ts
var n = 300 * 1e3, r = class {
	chainId;
	contractAddress;
	snapshotsPromise;
	lastFetchedAt;
	cacheDurationMs;
	constructor(e, t, r = n) {
		this.chainId = e, this.contractAddress = t, this.cacheDurationMs = r;
	}
	async fetchSnapshots() {
		return this.lastFetchedAt = Date.now(), t(this.chainId);
	}
	async ensureSnapshots(t = !1) {
		let n = Date.now(), r = !this.lastFetchedAt || n - this.lastFetchedAt > this.cacheDurationMs;
		(t || !this.snapshotsPromise || r) && (this.snapshotsPromise = this.fetchSnapshots());
		let i = await this.snapshotsPromise;
		if (!e(i.commitments.hinkalAddress, this.contractAddress) || i.commitments.chainId !== this.chainId) throw Error("Snapshots: incorrect contract or chain id");
		return i;
	}
	async getAll(e = !1) {
		return this.ensureSnapshots(e);
	}
	async getCommitments(e = !1) {
		return (await this.ensureSnapshots(e)).commitments;
	}
	async getAccessTokens(e = !1) {
		return (await this.ensureSnapshots(e)).accessTokens;
	}
	async getNullifiers(e = !1) {
		return (await this.ensureSnapshots(e)).nullifiers;
	}
};
//#endregion
export { r as SnapshotFetcherService };
