"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetTokenChartUseCase = void 0;
const not_found_error_1 = require("../../../domain/errors/not-found-error");
const TIMEFRAME_CONFIG = {
    LIVE: { interval: '5m', count: 24 },
    '1m': { interval: '5m', count: 12 },
    '1H': { interval: '5m', count: 12 },
    '1D': { interval: 'hourly', count: 24 },
    '1W': { interval: 'daily', count: 7 },
    '1M': { interval: 'daily', count: 30 },
};
class GetTokenChartUseCase {
    tokenRepository;
    coinMarketCapClient;
    constructor(tokenRepository, coinMarketCapClient) {
        this.tokenRepository = tokenRepository;
        this.coinMarketCapClient = coinMarketCapClient;
    }
    async execute(input) {
        const token = await this.tokenRepository.findById(input.tokenId);
        if (!token) {
            throw new not_found_error_1.NotFoundError('Token', input.tokenId);
        }
        const config = TIMEFRAME_CONFIG[input.timeframe] ?? { interval: 'daily', count: 7 };
        const cmcId = await this.coinMarketCapClient.getCoinIdByToken(token.name, token.symbol, token.isNative);
        if (!cmcId) {
            return this.generateFallbackChart(config.count);
        }
        return this.coinMarketCapClient.getPriceHistory(cmcId, config.interval, config.count);
    }
    generateFallbackChart(count) {
        const now = Date.now();
        const intervalMs = 24 * 60 * 60_000 / Math.max(count, 1);
        const points = [];
        for (let i = count - 1; i >= 0; i--) {
            const timestamp = new Date(now - i * intervalMs).toISOString();
            const price = Math.random() * 100;
            points.push({ timestamp, price });
        }
        return points;
    }
}
exports.GetTokenChartUseCase = GetTokenChartUseCase;
//# sourceMappingURL=get-token-chart.use-case.js.map