import { getEthPriceUsd, getTokenPriceUsd } from "./dexscreener-service.mjs";
//#region extensions/crypto/src/services/price-service.ts
/**
* Price Service — unified price feed shared across tools.
*
* Used by manage-orders (trigger checks), defi-swap (pre-swap display),
* defi-balance (ETH valuation), and the workflow orchestrator.
*
* Sources: DexScreener (primary), CoinGecko (fallback).
* Includes a TTL cache to avoid hammering APIs during multi-tool workflows.
*/
const CACHE_TTL_MS = 3e4;
const MAX_CACHE_SIZE = 500;
const _cache = /* @__PURE__ */ new Map();
function cacheKey(token, chain) {
	return `${chain}:${token.toLowerCase()}`;
}
function getCached(token, chain) {
	const entry = _cache.get(cacheKey(token, chain));
	if (!entry) return null;
	if (Date.now() - entry.fetchedAt > CACHE_TTL_MS) {
		_cache.delete(cacheKey(token, chain));
		return null;
	}
	return entry;
}
function setCache(token, chain, entry) {
	if (_cache.size >= MAX_CACHE_SIZE) {
		const now = Date.now();
		for (const [key, val] of _cache) if (now - val.fetchedAt > CACHE_TTL_MS) _cache.delete(key);
		if (_cache.size >= MAX_CACHE_SIZE) {
			const toDelete = _cache.size - MAX_CACHE_SIZE + 50;
			let deleted = 0;
			for (const key of _cache.keys()) {
				if (deleted >= toDelete) break;
				_cache.delete(key);
				deleted++;
			}
		}
	}
	_cache.set(cacheKey(token, chain), {
		...entry,
		fetchedAt: Date.now()
	});
}
/**
* Get current price for any token. Cached for 30s.
*/
async function getPrice(token, chain = "base") {
	const cached = getCached(token, chain);
	if (cached) {
		const ethPrice = await getEthPrice();
		return {
			priceUsd: cached.priceUsd,
			priceEth: ethPrice > 0 ? cached.priceUsd / ethPrice : 0,
			symbol: cached.pair?.baseToken?.symbol ?? token,
			name: cached.pair?.baseToken?.name ?? "",
			address: cached.pair?.baseToken?.address,
			change24h: cached.pair?.priceChange?.h24,
			volume24h: cached.pair?.volume?.h24,
			liquidity: cached.pair?.liquidity?.usd,
			source: "cache"
		};
	}
	const { priceUsd, pair } = await getTokenPriceUsd(token, chain);
	setCache(token, chain, {
		priceUsd,
		pair
	});
	const ethPrice = await getEthPrice();
	return {
		priceUsd,
		priceEth: ethPrice > 0 ? priceUsd / ethPrice : 0,
		symbol: pair?.baseToken?.symbol ?? token,
		name: pair?.baseToken?.name ?? "",
		address: pair?.baseToken?.address,
		change24h: pair?.priceChange?.h24,
		volume24h: pair?.volume?.h24,
		liquidity: pair?.liquidity?.usd,
		source: "dexscreener"
	};
}
/**
* Get ETH price in USD. Cached.
*/
let _ethPriceCache = null;
async function getEthPrice() {
	if (_ethPriceCache && Date.now() - _ethPriceCache.at < CACHE_TTL_MS) return _ethPriceCache.price;
	const price = await getEthPriceUsd();
	_ethPriceCache = {
		price,
		at: Date.now()
	};
	return price;
}
/**
* Get prices for multiple tokens at once (batched, cached).
*/
async function getPrices(tokens) {
	const results = /* @__PURE__ */ new Map();
	await Promise.all(tokens.map(async ({ token, chain }) => {
		try {
			const price = await getPrice(token, chain ?? "base");
			results.set(token, price);
		} catch {}
	}));
	return results;
}
/**
* Clear the price cache. Useful for testing.
*/
function clearPriceCache() {
	_cache.clear();
	_ethPriceCache = null;
}
//#endregion
export { clearPriceCache, getEthPrice, getPrice, getPrices };

//# sourceMappingURL=price-service.mjs.map