import { Logger as e } from "../../error-handling/logger.mjs";
import { getStateKey as t } from "../../functions/utils/string.utils.mjs";
import "../../functions/utils/index.mjs";
import { get as n, update as r } from "idb-keyval";
var i = new class {
	balancesRecordLabel = "walletsBalances";
	async getBalances() {
		return await n(this.balancesRecordLabel);
	}
	async getBalancesByWallet(e, n) {
		let r = await this.getBalances();
		if (r) return r[t(e, n)];
	}
	async setBalances(n, i, a, o) {
		let s = [];
		if (!o) return await r(this.balancesRecordLabel, (e = {}) => {
			let r = t(i, n);
			return {
				...e,
				[r]: a
			};
		}), a;
		if (o.length === 0) return await this.getBalancesByWallet(i, n) ?? [];
		try {
			await r(this.balancesRecordLabel, (e = {}) => {
				let r = t(i, n), c = this.createBalanceLookup(e[r]), l = this.createBalanceLookup(a);
				return o.forEach(({ erc20TokenAddress: e }) => {
					let t = e.toLowerCase();
					t in l ? c[t] = l[t] : delete c[t];
				}), s = Object.values(c), {
					...e,
					[r]: s
				};
			});
		} catch (t) {
			e.error("Failed to update wallet balances:", t);
		}
		return s;
	}
	createBalanceLookup(e = []) {
		return e.reduce((e, t) => ({
			...e,
			[t.token.erc20TokenAddress.toLowerCase()]: t
		}), {});
	}
}();
//#endregion
export { i as publicBalancesStorage };
