import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Box, Text } from "ink";
import { getToolSuccessRate } from "../session-summary.js";
import { formatDuration } from "../duration-format.js";
import { useTheme } from "../theme/theme.js";
function formatCount(value) {
    return (value ?? 0).toLocaleString();
}
function SummaryRow({ label, children }) {
    const theme = useTheme();
    return (_jsxs(Box, { children: [_jsx(Box, { width: 18, flexShrink: 0, children: _jsx(Text, { color: theme.link, children: label }) }), _jsx(Box, { flexShrink: 1, children: children })] }));
}
function SummarySection({ title, children }) {
    const theme = useTheme();
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: theme.text, bold: true, children: title }), children] }));
}
function topTools(summary) {
    const entries = Object.entries(summary.tools.byName)
        .sort(([, a], [, b]) => b.calls - a.calls || b.durationMs - a.durationMs)
        .slice(0, 5)
        .map(([name, stats]) => `${name} ×${stats.calls}`);
    return entries.length > 0 ? entries.join(", ") : "none";
}
export function SessionSummaryDisplay({ summary }) {
    const theme = useTheme();
    const successRate = getToolSuccessRate(summary.tools);
    const cacheTokens = (summary.usage.cacheRead ?? 0) + (summary.usage.cacheWrite ?? 0);
    const changedLines = summary.linesChanged.added > 0 || summary.linesChanged.removed > 0;
    return (_jsx(Box, { paddingLeft: 1, flexShrink: 1, children: _jsxs(Box, { flexDirection: "column", flexShrink: 1, borderStyle: "round", borderColor: theme.border, paddingX: 2, paddingY: 1, children: [_jsx(Text, { color: theme.secondary, bold: true, children: summary.title }), _jsxs(SummarySection, { title: "Session", children: [summary.sessionId && (_jsx(SummaryRow, { label: "ID", children: _jsx(Text, { color: theme.textDim, wrap: "truncate-end", children: summary.sessionId }) })), _jsx(SummaryRow, { label: "Model", children: _jsxs(Text, { color: theme.text, wrap: "truncate-end", children: [summary.provider, ":", summary.model] }) }), _jsx(SummaryRow, { label: "Directory", children: _jsx(Text, { color: theme.textDim, wrap: "truncate-end", children: summary.cwd }) })] }), _jsxs(SummarySection, { title: "Usage", children: [_jsx(SummaryRow, { label: "Wall time", children: _jsx(Text, { color: theme.text, children: formatDuration(summary.wallDurationMs) }) }), _jsx(SummaryRow, { label: "Turns", children: _jsx(Text, { color: theme.text, children: summary.turns.toLocaleString() }) }), _jsx(SummaryRow, { label: "Tokens", children: _jsxs(Text, { color: theme.text, children: [formatCount(summary.usage.inputTokens), " in /", " ", formatCount(summary.usage.outputTokens), " out", cacheTokens > 0 ? (_jsxs(Text, { color: theme.textDim, children: [" / ", formatCount(cacheTokens), " cache"] })) : null] }) })] }), _jsxs(SummarySection, { title: "Work", children: [_jsx(SummaryRow, { label: "Tool calls", children: _jsxs(Text, { color: theme.text, children: [summary.tools.totalCalls.toLocaleString(), " ", "(", _jsxs(Text, { color: theme.success, children: ["\u2713 ", summary.tools.totalSuccess.toLocaleString()] }), " ", _jsxs(Text, { color: theme.error, children: ["\u00D7 ", summary.tools.totalFail.toLocaleString()] }), successRate == null ? null : (_jsxs(Text, { color: theme.textDim, children: [" \u00B7 ", successRate.toFixed(1), "%"] })), ")"] }) }), _jsx(SummaryRow, { label: "Top tools", children: _jsx(Text, { color: theme.textDim, wrap: "wrap", children: topTools(summary) }) }), summary.serverToolCalls > 0 && (_jsx(SummaryRow, { label: "Server tools", children: _jsx(Text, { color: theme.text, children: summary.serverToolCalls.toLocaleString() }) })), changedLines && (_jsx(SummaryRow, { label: "Code changes", children: _jsxs(Text, { children: [_jsxs(Text, { color: theme.success, children: ["+", summary.linesChanged.added.toLocaleString()] }), " ", _jsxs(Text, { color: theme.error, children: ["-", summary.linesChanged.removed.toLocaleString()] })] }) }))] }), summary.footer && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.textMuted, wrap: "wrap", children: summary.footer }) }))] }) }));
}
//# sourceMappingURL=SessionSummary.js.map