"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsoleRenderer = void 0;
const events_1 = require("events");
class ConsoleRenderer {
    activeTaskId;
    emitter = new events_1.EventEmitter();
    paused = false;
    bufferedEvents = [];
    setActiveTaskId(taskId) {
        this.activeTaskId = taskId;
    }
    getActiveTaskId() {
        return this.activeTaskId;
    }
    isActiveTask(taskId) {
        if (!this.activeTaskId)
            return true;
        return taskId === this.activeTaskId;
    }
    onLog(handler) {
        this.emitter.on("log", handler);
    }
    onAgentStatus(handler) {
        this.emitter.on("agentStatus", handler);
    }
    onToolCall(handler) {
        this.emitter.on("toolCall", handler);
    }
    onToolResult(handler) {
        this.emitter.on("toolResult", handler);
    }
    onAgentMessage(handler) {
        this.emitter.on("agentMessage", handler);
    }
    onAgentDone(handler) {
        this.emitter.on("agentDone", handler);
    }
    pause() {
        this.paused = true;
    }
    resume() {
        this.paused = false;
        const buffered = this.bufferedEvents.splice(0);
        for (const event of buffered) {
            this.render(event);
        }
    }
    render(event) {
        if (!this.isActiveTask(event.taskId))
            return;
        if (this.paused) {
            this.bufferedEvents.push(event);
            return;
        }
        switch (event.type) {
            case "log":
                this.emitter.emit("log", event);
                this.renderLog(event);
                break;
            case "agentStatus":
                this.emitter.emit("agentStatus", event);
                this.renderAgentStatus(event);
                break;
            case "toolCall":
                this.emitter.emit("toolCall", event);
                this.renderToolCall(event);
                break;
            case "toolResult":
                this.emitter.emit("toolResult", event);
                this.renderToolResult(event);
                break;
            case "agentMessage":
                this.emitter.emit("agentMessage", event);
                this.renderAgentMessage(event);
                break;
            case "agentDone":
                this.emitter.emit("agentDone", event);
                this.renderAgentDone(event);
                break;
        }
    }
    logMessages(events, count = 20) {
        const recent = events.slice(-count);
        console.log(`\n📜 Last ${recent.length} messages:`);
        console.log("─".repeat(60));
        for (const event of recent) {
            this.render(event);
        }
        console.log("─".repeat(60));
    }
    renderLog(event) {
        switch (event.level) {
            case "warn":
                console.warn(event.message);
                break;
            case "error":
                console.error(event.message);
                break;
            default:
                console.log(event.message);
        }
    }
    renderAgentStatus(event) {
        console.log(`\n● ${event.agentName} status: ${event.statusMessage}`);
    }
    renderToolCall(event) {
        console.time(JSON.stringify(event.toolCall.function.name));
        console.log(` 🔨 Tool: ${event.toolCall.function.name}\n Args: ${event.toolCall.function.arguments}\n`);
    }
    renderToolResult(event) {
        console.timeEnd(JSON.stringify(event.toolCall.function.name));
        console.log(` 🔨 Tool Response:\n              ${JSON.stringify(event.result, null, 2)}`);
    }
    renderAgentMessage(event) {
        console.log(`[${event.agentName}]: ${event.message}`);
    }
    renderAgentDone(event) {
        console.log(`🎯 Agent Done: ${event.agentName}`);
        if (event.totalCost) {
            console.log(`💰 Total cost: $${event.totalCost.toFixed(4)}`);
        }
    }
}
exports.ConsoleRenderer = ConsoleRenderer;
//# sourceMappingURL=ConsoleRenderer.js.map