import { Logger as e } from "../../error-handling/logger.mjs";
import { isSameERC20Token as t } from "../../functions/utils/token-check.utils.mjs";
import { get as n, update as r } from "idb-keyval";
var i = new class {
	STORAGE_KEY = "hiddenTokenAddresses";
	UNHIDDEN_SPAM_KEY = "unhiddenSpamTokens";
	async getHiddenTokens(t) {
		try {
			return (await n(this.STORAGE_KEY))?.[t] || [];
		} catch (t) {
			return e.error("Error retrieving hidden tokens", t), [];
		}
	}
	async getHiddenTokenAddresses(t) {
		try {
			return (await this.getHiddenTokens(t)).map(({ erc20TokenAddress: e }) => e.toLowerCase());
		} catch (t) {
			return e.error("Error retrieving hidden tokens", t), [];
		}
	}
	async filterOutHiddenTokens(e, n) {
		let r = await this.getHiddenTokens(e);
		return n.filter((e) => !r.some((n) => t(n, e)));
	}
	async hideToken(n) {
		try {
			await r(this.STORAGE_KEY, (e) => {
				let r = e ?? {}, { chainId: i } = n;
				return r[i] || (r[i] = []), r[i].some((e) => t(e, n)) || r[i].push(n), r;
			}), n.isSpam && await this.deleteUnhiddenSpamToken(n);
		} catch (t) {
			e.error("Error hiding token", t);
		}
	}
	async showToken(n) {
		try {
			await r(this.STORAGE_KEY, (e) => {
				let { chainId: r } = n, i = e ?? {};
				return i[r] || (i[r] = []), i[r] = i[r].filter((e) => !t(e, n)), i;
			}), n.isSpam && await this.storeUnhiddenSpamToken(n);
		} catch (t) {
			e.error("Error showing token", t);
		}
	}
	async storeUnhiddenSpamToken(n) {
		let { chainId: i } = n;
		try {
			await r(this.UNHIDDEN_SPAM_KEY, (e) => {
				let r = e ?? {};
				return r[i] || (r[i] = []), r[i].some((e) => t(e, n)) || r[i].push(n), r;
			});
		} catch (t) {
			e.error("Error storing unhidden spam token", t);
		}
	}
	async deleteUnhiddenSpamToken(e) {
		await r(this.UNHIDDEN_SPAM_KEY, (n) => {
			let { chainId: r } = e, i = n ?? {};
			return i[r] = i[r]?.filter((n) => !t(n, e)), i;
		});
	}
	async getUnhiddenSpamTokens(t) {
		try {
			return (await n(this.UNHIDDEN_SPAM_KEY))?.[t] || [];
		} catch (t) {
			return e.error("Error retrieving unhidden spam tokens", t), [];
		}
	}
	async hideUnnecessarySpamTokens(e, n) {
		let r = await this.getUnhiddenSpamTokens(e), i = n.filter((e) => e.isSpam ? !r.some((n) => t(n, e)) : !1);
		await Promise.all(i.map((e) => this.hideToken(e)));
	}
	async hideSpamsAndFetchHiddenTokensAddresses(e, t) {
		return await this.hideUnnecessarySpamTokens(e, t), this.getHiddenTokenAddresses(e);
	}
}();
//#endregion
export { i as tokenVisibilityStorage };
