import { PrivateBalanceUpdateType as e } from "../../types/balances.types.mjs";
import { checkAddressNotUpdated as t } from "./upToDateState.mjs";
import { Logger as n } from "../../error-handling/logger.mjs";
import { resetStateMutex as r } from "../../mutexes/mutex.mjs";
import { balanceChangedHandler as i } from "../web3/events/balanceChangedHandler.mjs";
//#region libs/shared/common/src/functions/utils/dispatchBalanceRefreshes.ts
var a = async (a, o = !1) => {
	let s = await a.getEthereumAddress();
	i({
		updateType: e.OnlyCache,
		balanceChangeState: { ethereumAddress: s }
	}), await r.runExclusive(async () => {
		try {
			await t(a, s), o || (await a.resetMerkle(), await t(a, s)), i({
				updateType: e.FreshWithoutLoader,
				balanceChangeState: {
					ethereumAddress: s,
					userKeys: a.userKeys
				},
				updateTokensListBefore: !0
			});
		} catch (e) {
			n.log("resetMerkleAndRefreshPrivateBalance error", e);
		}
	});
}, o = async (n, a = !1) => {
	let o = await n.getEthereumAddress();
	await r.runExclusive(async () => {
		let { userKeys: r } = n;
		await t(n, o), await n.resetMerkleTreesIfNecessary(), await t(n, o), i({
			updateType: e.Fresh,
			balanceChangeState: {
				ethereumAddress: o,
				userKeys: r
			},
			updateTokensListBefore: !0,
			resetCacheBefore: a
		});
	});
}, s = async (e) => {
	await o(e, !0);
};
//#endregion
export { s as hardRefreshPrivateBalance, a as initialMerkleAndPrivateBalanceRefresh, o as refreshPrivateBalance };
