import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { inferToolResultTone, toolResultBody, toolResultTitle } from "./ToolResultMessage.js";
import { parseToolStartText } from "./ToolStartMessage.js";
import { MessageResponse } from "./MessageResponse.js";
import { parseToolResultDetail } from "./ToolResultDetail.js";
import { StatusIcon } from "./design/StatusIcon.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
export function ToolActivityGroup(props) {
    const summary = buildToolActivitySummary(props.items, props.detailWidth);
    return (_jsx(MessageResponse, { tone: summary.tone, children: _jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusIcon, { state: summary.state, withSpace: true }), _jsx(Text, { color: toneColor(summary.tone), children: summary.title })] }), _jsx(Text, { color: "gray", children: formatToolActivityCounts(summary) }), summary.latest.map((line, index) => (_jsx(Text, { color: "gray", children: `  ${line}` }, `${index}-${line}`)))] }) }));
}
export function buildToolActivitySummary(items, detailWidth = 96) {
    const records = items.map((item) => parseToolActivityRecord(item, detailWidth));
    const counts = actionCounts(records);
    const running = records.filter((record) => record.status === "running" || record.status === "started").length;
    const failed = records.filter((record) => record.tone === "error").length;
    const warnings = records.filter((record) => record.tone === "warning").length;
    const succeeded = records.filter((record) => record.tone === "success").length;
    const tone = failed > 0 ? "error" : warnings > 0 || running > 0 ? "warning" : succeeded > 0 ? "success" : "default";
    const state = failed > 0 ? "error" : running > 0 ? "loading" : warnings > 0 ? "warning" : "success";
    const total = records.length;
    return {
        total,
        running,
        succeeded,
        failed,
        warnings,
        tone,
        state,
        title: `${running > 0 ? "Running" : "Ran"} ${total} tool event${total === 1 ? "" : "s"}`,
        counts,
        latest: records.slice(-2).map((record) => formatToolActivityRecord(record, detailWidth)),
    };
}
export function parseToolActivityRecord(item, detailWidth = 96) {
    if (item.role === "tool-start") {
        const info = parseToolStartText(item.text, detailWidth);
        return {
            action: info.name,
            status: "running",
            ...(info.detail ? { detail: info.detail } : {}),
            tone: "warning",
        };
    }
    const title = toolResultTitle(item.text);
    const body = toolResultBody(item.text);
    const detail = parseToolResultDetail(title, body);
    const tone = inferToolResultTone(item.text);
    if (detail) {
        return {
            action: detail.action,
            status: detail.status,
            ...(detail.target ? { detail: detail.target } : detail.message ? { detail: detail.message } : {}),
            tone,
        };
    }
    const [action = "tool", status = tone === "default" ? "done" : tone] = title.split(/\s+/);
    return {
        action,
        status,
        ...(body ? { detail: body } : {}),
        tone,
    };
}
export function formatToolActivityCounts(summary) {
    const actionSummary = summary.counts
        .slice(0, 4)
        .map((entry) => `${entry.action} ${entry.count}`)
        .join(", ");
    const statusParts = [
        summary.running > 0 ? `running ${summary.running}` : "",
        summary.succeeded > 0 ? `ok ${summary.succeeded}` : "",
        summary.failed > 0 ? `failed ${summary.failed}` : "",
        summary.warnings > 0 ? `needs attention ${summary.warnings}` : "",
    ].filter(Boolean);
    return [actionSummary, statusParts.join(" / ")].filter(Boolean).join(" | ");
}
export function estimateToolActivityGroupRows(items) {
    if (items.length === 0)
        return 0;
    return 3 + Math.min(2, items.length);
}
function actionCounts(records) {
    const counts = new Map();
    for (const record of records) {
        counts.set(record.action, (counts.get(record.action) ?? 0) + 1);
    }
    return [...counts.entries()]
        .map(([action, count]) => ({ action, count }))
        .sort((a, b) => b.count - a.count || a.action.localeCompare(b.action));
}
function formatToolActivityRecord(record, width) {
    const detail = record.detail ? ` ${record.detail}` : "";
    return truncateCells(`${record.action} ${record.status}${detail}`, width);
}
