import { API as e } from "../../API/API.mjs";
import { getTokenStateKey as t } from "../../functions/utils/string.utils.mjs";
import { get as n, update as r } from "idb-keyval";
var i = new class {
	tokenPricesRecordLabel = "tokenPrices";
	async getPrices() {
		return await n(this.tokenPricesRecordLabel);
	}
	async updatePrices(n, i) {
		if (!i.length) return [];
		let { prices: a } = await e.getTokenPrices(n, i);
		return await r(this.tokenPricesRecordLabel, (e = {}) => {
			let r = { ...e };
			return a.forEach((e, a) => {
				r[t(i[a], n)] = e;
			}), r;
		}), a;
	}
	async getAndUpdatePrices(e, n) {
		let r = await this.getPrices(), i = Array(n.length).fill(0);
		if (!r) i = await this.updatePrices(e, n);
		else {
			let a = [], o = [];
			n.forEach((n, s) => {
				let c = t(n, e);
				r[c] ? i[s] = r[c] : (a.push(n), o.push(s));
			});
			let s = await this.updatePrices(e, a);
			o.forEach((e, t) => {
				i[e] = s[t];
			});
		}
		return i;
	}
	async updateCachedPricesByChain(n) {
		let i = await this.getPrices();
		if (!i || Object.keys(i).length === 0) return;
		let a = Object.entries(i).filter(([e]) => {
			let [, t] = e.split("-");
			return parseInt(t, 10) === n;
		}).map(([e]) => e.split("-")[0]);
		if (a.length === 0) return;
		let { prices: o } = await e.getTokenPrices(n, a);
		await r(this.tokenPricesRecordLabel, (e = {}) => {
			let r = { ...e };
			return o.forEach((e, i) => {
				let o = t(a[i], n);
				r[o] = e;
			}), r;
		});
	}
}();
//#endregion
export { i as pricesStorage };
