import { n as __exportAll } from "./chunk-BEJ448es.mjs";
import { n as getEthPriceUsd, r as getTokenPriceUsd } from "./dexscreener-service-BSUiBkYa.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.
*/
var price_service_exports = /* @__PURE__ */ __exportAll({
	getEthPrice: () => getEthPrice,
	getPrice: () => getPrice
});
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;
}
//#endregion
export { getPrice as n, price_service_exports as r, getEthPrice as t };

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