import { StorageKeys as e } from "../../types/cache.types.mjs";
import { lowerCaseIncludes as t } from "../../functions/utils/caseInsensitive.utils.mjs";
import { API as n } from "../../API/API.mjs";
import { Logger as r } from "../../error-handling/logger.mjs";
import { getStateKey as i } from "../../functions/utils/string.utils.mjs";
import { tokenVisibilityStorage as a } from "./token-visibility-db.mjs";
import { get as o, update as s } from "idb-keyval";
//#region libs/shared/common/src/data-structures/TokenDBs/PrivateTokensDB.ts
var c = class {
	fetchedTokens = {};
	initalFetched = !1;
	async updateFetchedTokens() {
		let t = await o(e.PRIVATE_TOKENS);
		this.fetchedTokens = t ?? {}, this.initalFetched = !0;
	}
	async getPrivateTokens(t, n) {
		let r = i(n, t);
		return (await o(e.PRIVATE_TOKENS))?.[r] || [];
	}
	async fetchPrivateTokens(e, i, a) {
		try {
			let r = [...new Set(i)].filter((e) => e && !t(a, e));
			if (!r.length) return [];
			let o = (await n.tokensInfoCall(e, r)).filter((e) => !!e), s = /* @__PURE__ */ new Map();
			return o.forEach((e) => {
				s.set(e.erc20TokenAddress.toLowerCase(), e);
			}), Array.from(s.values());
		} catch (e) {
			return r.error("Error fetching private tokens:", e), [];
		}
	}
	async fetchAndUpdatePrivateTokens(n, r, o, c) {
		let l = (await this.getPrivateTokens(r, o)).map((e) => e.erc20TokenAddress), u = c ?? await this.fetchPrivateTokens(r, n, l), d = await a.hideSpamsAndFetchHiddenTokensAddresses(r, u), f = i(o, r);
		await s(e.PRIVATE_TOKENS, (e = {}) => {
			let n = e?.[f] || [], r = n.map((e) => e.erc20TokenAddress), i = u.filter((e) => !t(r, e.erc20TokenAddress));
			n.push(...i);
			let a = n.filter((e) => !t(d, e.erc20TokenAddress)), o = {
				...e,
				[f]: a
			};
			return this.fetchedTokens = o, o;
		});
	}
	getFetchedTokens(e, t) {
		let n = i(t, e);
		return this.fetchedTokens[n] || [];
	}
}, l = new c();
//#endregion
export { c as PrivateTokensDB, l as privateTokensDB };
