"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TruPulseToolService = void 0;
/**
 * TruPulse Tool Service
 * Shared business logic for TruPulse tools that can be used by both API routes and MCP server
 */
const logger_1 = __importDefault(require("../../../../logger"));
const clickhouseQueries_1 = require("../utils/clickhouseQueries");
const defaultFilters_1 = require("../utils/defaultFilters");
/**
 * TruPulse Tool Service
 */
class TruPulseToolService {
    /**
     * Apply default filters helper
     */
    static applyDefaults(filters, limit, sorting) {
        return (0, defaultFilters_1.applyDefaultFilters)(filters, limit, sorting);
    }
    /**
     * Get multiple traces by their IDs
     */
    static async getTracesByIds(params) {
        try {
            const { projectId, traceIds, limit } = params;
            if (!projectId) {
                return {
                    success: false,
                    message: 'projectId is required',
                };
            }
            if (!traceIds || traceIds.length === 0) {
                return {
                    success: false,
                    message: 'traceIds is required (array of trace IDs)',
                };
            }
            const finalLimit = limit || 100;
            const queryParams = {
                projectId,
                traceIds: traceIds.slice(0, finalLimit),
            };
            const traces = await (0, clickhouseQueries_1.queryTraces)(queryParams);
            return {
                success: true,
                data: {
                    traces,
                    count: traces.length,
                },
            };
        }
        catch (error) {
            logger_1.default.error(`Get traces by IDs error: ${error}`);
            return {
                success: false,
                message: error instanceof Error ? error.message : 'Internal server error',
            };
        }
    }
    /**
     * Get multiple spans by their IDs
     */
    static async getSpansByIds(params) {
        try {
            const { projectId, spanIds, limit } = params;
            if (!projectId) {
                return {
                    success: false,
                    message: 'projectId is required',
                };
            }
            if (!spanIds || spanIds.length === 0) {
                return {
                    success: false,
                    message: 'spanIds is required (array of span IDs)',
                };
            }
            const finalLimit = limit || 100;
            const queryParams = {
                projectId,
                spanIds: spanIds.slice(0, finalLimit),
            };
            const spans = await (0, clickhouseQueries_1.querySpans)(queryParams);
            return {
                success: true,
                data: {
                    spans,
                    count: spans.length,
                },
            };
        }
        catch (error) {
            logger_1.default.error(`Get spans by IDs error: ${error}`);
            return {
                success: false,
                message: error instanceof Error ? error.message : 'Internal server error',
            };
        }
    }
    /**
     * Get a single span by ID
     */
    static async getSpanById(params) {
        try {
            const { projectId, spanId, filters } = params;
            if (!projectId) {
                return {
                    success: false,
                    message: 'projectId is required',
                };
            }
            if (!spanId) {
                return {
                    success: false,
                    message: 'spanId is required',
                };
            }
            const span = await (0, clickhouseQueries_1.querySpanById)({
                projectId,
                spanId,
                evalResultsFilters: filters?.evalResultsFilters,
            });
            if (!span) {
                return {
                    success: false,
                    message: 'Span not found',
                };
            }
            return {
                success: true,
                data: {
                    span,
                },
            };
        }
        catch (error) {
            logger_1.default.error(`Get span by ID error: ${error}`);
            return {
                success: false,
                message: error instanceof Error ? error.message : 'Internal server error',
            };
        }
    }
    /**
     * Get multiple sessions by their IDs
     */
    static async getSessionsByIds(params) {
        try {
            const { projectId, sessionIds, limit } = params;
            if (!projectId) {
                return {
                    success: false,
                    message: 'projectId is required',
                };
            }
            if (!sessionIds || sessionIds.length === 0) {
                return {
                    success: false,
                    message: 'sessionIds is required (array of session IDs)',
                };
            }
            const finalLimit = limit || 100;
            const queryParams = {
                projectId,
                sessionIds: sessionIds.slice(0, finalLimit),
            };
            const sessions = await (0, clickhouseQueries_1.querySessions)(queryParams);
            return {
                success: true,
                data: {
                    sessions,
                    count: sessions.length,
                },
            };
        }
        catch (error) {
            logger_1.default.error(`Get sessions by IDs error: ${error}`);
            return {
                success: false,
                message: error instanceof Error ? error.message : 'Internal server error',
            };
        }
    }
}
exports.TruPulseToolService = TruPulseToolService;
//# sourceMappingURL=truPulseToolService.js.map