import { color } from "../components/color.js";
import { resolveOutputFormat } from "../internal/output-format.js";
import { renderMarkdown } from "../terminal-markdown/index.js";
import { getAcpWriter } from "./writer.js";
function truncate(text, maxLength) {
    if (text.length <= maxLength)
        return text;
    if (maxLength <= 3)
        return text.slice(0, maxLength);
    return `${text.slice(0, maxLength - 3)}...`;
}
const KIND_COLORS = {
    exec: (text) => color.yellow(text),
    edit: (text) => color.magenta(text),
    read: (text) => color.cyan(text),
    search: (text) => color.blue(text),
    think: (text) => color.dim(text),
    other: (text) => color.dim(text)
};
function colorForKind(kind) {
    return Object.prototype.hasOwnProperty.call(KIND_COLORS, kind)
        ? KIND_COLORS[kind]
        : (text) => color.dim(text);
}
function writeLine(line) {
    getAcpWriter()(line);
}
function agentPrefix() {
    return `${color.green.bold("✓")} agent: `;
}
function formatCost(costUsd) {
    return new Intl.NumberFormat("en-US", {
        style: "currency",
        currency: "USD",
        minimumFractionDigits: 2,
        maximumFractionDigits: 6
    }).format(costUsd);
}
export function renderAgentMessage(text) {
    const format = resolveOutputFormat();
    if (format === "markdown") {
        writeLine(`- **agent:** ${text}`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({ event: "agent_message", text }));
        return;
    }
    const rendered = renderMarkdown(text).trimEnd();
    writeLine(`${agentPrefix()}${rendered}`);
}
export function renderToolStart(kind, title) {
    const format = resolveOutputFormat();
    if (format === "markdown") {
        writeLine(`- *→ ${kind}: ${title}*`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({ event: "tool_start", kind, title }));
        return;
    }
    const color = colorForKind(kind);
    writeLine(color(`  → ${kind}: ${title}`));
}
export function renderToolComplete(kind) {
    const format = resolveOutputFormat();
    if (format === "markdown") {
        writeLine(`- *✓ ${kind}*`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({ event: "tool_complete", kind }));
        return;
    }
    const color = colorForKind(kind);
    writeLine(color(`  ✓ ${kind}`));
}
export function renderReasoning(text) {
    const format = resolveOutputFormat();
    if (format === "markdown") {
        writeLine(`- *thinking:* ${truncate(text, 80)}`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({ event: "reasoning", text }));
        return;
    }
    writeLine(color.dim(`  ✓ ${truncate(text, 80)}`));
}
export function renderUsage(tokens) {
    const format = resolveOutputFormat();
    const cached = typeof tokens.cached === "number" && tokens.cached > 0 ? ` (${tokens.cached} cached)` : "";
    let cost = "";
    if (typeof tokens.costUsd === "number") {
        cost = ` (${formatCost(tokens.costUsd)})`;
    }
    if (format === "markdown") {
        writeLine(`- **tokens:** ${tokens.input} in → ${tokens.output} out${cost}`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({
            event: "usage",
            inputTokens: tokens.input,
            outputTokens: tokens.output,
            cachedTokens: tokens.cached ?? 0,
            costUsd: tokens.costUsd ?? 0
        }));
        return;
    }
    writeLine("");
    writeLine(color.green(`✓ tokens: ${tokens.input} in${cached} → ${tokens.output} out${cost}`));
}
export function renderPermissionRejected(title) {
    const format = resolveOutputFormat();
    if (format === "markdown") {
        writeLine(`- **permission rejected:** ${title}`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({ event: "permission_rejected", title }));
        return;
    }
    writeLine(color.yellow(`  ✗ permission rejected: ${title}`));
}
export function renderError(message) {
    const format = resolveOutputFormat();
    if (format === "markdown") {
        writeLine(`- **error:** ${message}`);
        return;
    }
    if (format === "json") {
        writeLine(JSON.stringify({ event: "error", message }));
        return;
    }
    writeLine(color.red(`✗ ${message}`));
}
