/**
 * Legacy data.ts — thin shim over the provider registry.
 *
 * Keeps the historical `string | Data` error-by-string return convention so
 * the existing callers (`trading.ts`, `tools/index.ts` wiring into
 * `LiveExchange`) keep working without a call-site rewrite. New code should
 * import from `./providers/registry.js` and consume `PriceData | ProviderError`
 * directly — the structured error shape enables UI color-coding and retry
 * classification the string convention can't express.
 */
import { getProvider, getPriceProvider } from './providers/registry.js';
import { runFetcher } from './providers/fetcher.js';
import { isProviderError } from './providers/standard-models.js';
export function resolveId(ticker) {
    // Delegated — kept exported for backwards compat with any external caller.
    return ticker.toLowerCase();
}
/**
 * Look up a spot price. `assetClass` defaults to 'crypto' so all existing
 * crypto-only callers (`TradingSignal`, `LiveExchange.getPrice`) behave
 * exactly as before. Pass 'fx' / 'commodity' / 'stock' (plus a `market`
 * code for stocks) to hit the multi-asset Gateway endpoints.
 */
export async function getPrice(ticker, assetClass = 'crypto', market) {
    const result = await runFetcher(getPriceProvider(assetClass), { ticker, assetClass, market });
    if (isProviderError(result))
        return result.message;
    return {
        price: result.priceUsd,
        change24h: result.change24hPct,
        volume24h: result.volume24hUsd,
        marketCap: result.marketCapUsd,
    };
}
/** Convenience: FX pair lookup (e.g. "EUR-USD"). */
export async function getFxPrice(ticker) {
    return getPrice(ticker, 'fx');
}
/** Convenience: commodity lookup (e.g. "XAU-USD" for gold). */
export async function getCommodityPrice(ticker) {
    return getPrice(ticker, 'commodity');
}
/** Convenience: stock lookup (e.g. "AAPL" on market "us"). */
export async function getStockPrice(ticker, market) {
    return getPrice(ticker, 'stock', market);
}
export async function getOHLCV(ticker, days = 30) {
    const result = await runFetcher(getProvider('ohlcv'), { ticker, days });
    if (isProviderError(result))
        return result.message;
    return { closes: result.closes, timestamps: result.timestamps };
}
export async function getTrending() {
    const result = await runFetcher(getProvider('trending'), {});
    if (isProviderError(result))
        return result.message;
    return result.map(c => ({
        id: c.providerId,
        name: c.name,
        symbol: c.symbol,
        marketCapRank: c.marketCapRank,
    }));
}
export async function getMarketOverview() {
    const result = await runFetcher(getProvider('markets'), { limit: 20 });
    if (isProviderError(result))
        return result.message;
    return result.map(c => ({
        id: c.providerId,
        symbol: c.ticker.toLowerCase(),
        name: c.name,
        price: c.priceUsd,
        change24h: c.change24hPct,
        marketCap: c.marketCapUsd,
        volume24h: c.volume24hUsd,
    }));
}
