import { safeJsonStringify as e } from "./serialize.utils.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import { deserializeDecodedTxs as n, serializeDecodedTxs as r } from "./getDataFromTransaction.mjs";
import { get as i, set as a } from "idb-keyval";
//#region libs/shared/common/src/functions/utils/cacheFunctions.ts
var o = {
	encryptedOutputs: [],
	lastOutput: ""
}, s = (e, t) => ({
	shortPublicKey: e.substring(0, 25),
	shortHinkalAddress: t.substring(0, 25)
}), c = (e, t, n) => `hinkalCache-${e}-${t}-${n}`, l = (n, r, i) => {
	let { hinkalAddress: a } = t[r].contractData;
	if (!r || !i || !a) throw Error("GetHinkalCache: incorrect arguments");
	let { shortPublicKey: l, shortHinkalAddress: u } = s(i, a);
	return JSON.parse(n.cacheDevice.get(c(r, l, u)) ?? e(o));
}, u = (n, r, i, a) => {
	let { hinkalAddress: o } = t[i].contractData;
	if (!i || !a || !o) throw Error("SetHinkalCache: incorrect arguments");
	let { shortPublicKey: l, shortHinkalAddress: u } = s(a, o);
	r.cacheDevice.set(c(i, l, u), e(n));
}, d = (e, t, n) => {
	u(o, e, t, n);
}, f = (e, t, n) => `txsCache-${e}-${t}-${n}`, p = async (t, n, i, o, c, l) => {
	if (!n || !i || !o) throw Error("saveTxsCache: incorrect arguments");
	let { shortPublicKey: u, shortHinkalAddress: d } = s(i, o), p = r(t), m = f(n, u, d), h = e({
		serializedTxs: p,
		lastHash: c
	});
	try {
		await a(m, h);
	} catch {
		l.cacheDevice.set(m, h);
	}
}, m = {
	serializedTxs: [],
	lastHash: ""
}, h = async (t, r, a, o) => {
	if (!t || !r || !a) throw Error("loadTxsCache: incorrect arguments");
	let { shortPublicKey: c, shortHinkalAddress: l } = s(r, a), u = f(t, c, l), d;
	try {
		let e = await i(u);
		if (!e) throw Error("Empty indexedDB cache");
		d = e;
	} catch {
		d = o.cacheDevice.get(u);
	}
	let { serializedTxs: p, lastHash: h } = JSON.parse(d ?? e(m));
	return {
		decodedTxs: p.map((e) => n(e)),
		lastHash: h === "" ? void 0 : h
	};
};
//#endregion
export { c as getFilePath, l as getHinkalCache, h as loadTxsCache, d as resetCache, p as saveTxsCache, u as setHinkalCache };
