import { ActivityStatus as e } from "../../types/activities.types.mjs";
import { StorageKeys as t } from "../../types/cache.types.mjs";
import "../../types/index.mjs";
import { transactionErrorCodes as n } from "../../error-handling/error-codes.constants.mjs";
import { Logger as r } from "../../error-handling/logger.mjs";
import "../../error-handling/index.mjs";
import { RELAYER_TX_CONFIRMATION_TIMEOUT as i } from "../../constants/tx-confirmation.constants.mjs";
import { waitForTransactionConfirmation as a } from "../../functions/utils/tx-confirmation.utils.mjs";
import "../../functions/index.mjs";
import { get as o, update as s } from "idb-keyval";
//#region libs/shared/common/src/data-structures/IndexedDB/activity-db.ts
var c = class {
	generateTxId() {
		return `tx_${(/* @__PURE__ */ new Date()).getTime()}_${Math.random().toString(36).substring(2, 15)}`;
	}
	async getTransactionRecord() {
		return await o(t.TX_ACTIVITY) || {};
	}
	async saveTxRequest(n) {
		let { from: r, to: i, gasInDollar: a, extraNativeTokenFee: o, chainId: c, status: l, txHash: u, activityType: d, tokensReceive: f, tokensSpend: p, tokensApprove: m, activityIcon: h, dappUrl: g, dappName: _, integrationProvider: v, knownPartialSignature: y } = n, b = this.generateTxId(), x = {
			id: b,
			from: r,
			to: i,
			gasInDollar: a?.toString(),
			extraNativeTokenFee: o?.toString(),
			chainId: c,
			txHash: u,
			activityType: d,
			status: l ?? e.PENDING,
			timestamp: /* @__PURE__ */ new Date(),
			tokensReceive: f,
			tokensSpend: p,
			tokensApprove: m,
			activityIcon: h,
			dappUrl: g,
			dappName: _,
			integrationProvider: v,
			knownPartialSignature: y
		};
		return await s(t.TX_ACTIVITY, (e = {}) => (e[b] = x, e)), b;
	}
	async getTxRequest(e) {
		return (await this.getTransactionRecord())[e] || null;
	}
	async getAllTxRequests(e) {
		let t = await this.getTransactionRecord();
		return Object.values(t).filter((t) => t.chainId === e).sort((e, t) => new Date(t.timestamp).getTime() - new Date(e.timestamp).getTime());
	}
	async updateTxHash(e, n) {
		let r = await this.getTxRequest(e);
		if (!r) throw Error("Activity not found");
		let i = {
			...r,
			txHash: n
		};
		await s(t.TX_ACTIVITY, (t = {}) => (t[e] = i, t));
	}
	async updateTxStatusAndHash(e, n, r) {
		let i = await this.getTxRequest(e);
		if (!i) throw Error("Transaction not found");
		let a = {
			...i,
			status: n,
			txHash: r,
			timestamp: /* @__PURE__ */ new Date()
		};
		return await s(t.TX_ACTIVITY, (t = {}) => (t[e] = a, t)), a;
	}
	async updateActivity(e, t, n) {
		try {
			await this.updateTxStatusAndHash(e, t, n);
		} catch (e) {
			r.error("error in updating tx request", e);
		}
	}
	async removePendingTransactions(e) {
		try {
			await s(t.TX_ACTIVITY, (t = {}) => (delete t[e], t));
		} catch (e) {
			r.error("error in deleting tx requests", e);
		}
	}
	async updatePendingTransactions(t, o, s, c) {
		try {
			let r = await this.getTxRequest(o);
			if (!r) throw Error("Transaction not found");
			let { chainId: l } = r;
			if (!t.success) {
				c?.(t.error), await this.updateActivity(o, e.FAILED, "erroredTx" in t ? t.erroredTx : void 0);
				return;
			}
			if ("txHash" in t) {
				let { txHash: r } = t;
				try {
					await a(l, r, { evmTimeoutMs: i });
				} catch (t) {
					let i = t instanceof Error ? t : Error(n.UNKNOWN);
					c?.(i), await this.updateActivity(o, e.FAILED, r);
					return;
				}
				s?.(), await this.updateActivity(o, e.SUCCESSFUL, r);
			}
		} catch (e) {
			r.error("error in updating pending transactions", e);
		}
	}
}, l = new c();
//#endregion
export { c as ActivityStorage, l as activityStorage };
