import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { StatusIcon } from "./design/StatusIcon.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
export function ContextSuggestions(props) {
    const suggestions = props.suggestions ?? [];
    if (suggestions.length === 0)
        return null;
    const width = Math.max(40, props.width ?? 80);
    const models = suggestions.map(contextSuggestionModel);
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, marginBottom: 1, children: [_jsx(Text, { bold: true, children: "Suggestions" }), models.map((suggestion, index) => (_jsxs(Box, { flexDirection: "column", marginTop: index === 0 ? 0 : 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusIcon, { state: suggestion.severity, withSpace: true }), _jsx(Text, { bold: true, color: toneColor(suggestion.tone), children: truncateCells(suggestion.title, Math.max(12, width - 18)) }), suggestion.savingsLabel ? (_jsxs(Text, { color: toneColor("muted"), children: [" ", suggestion.savingsLabel] })) : null] }), _jsx(Box, { marginLeft: 2, children: _jsx(Text, { color: toneColor("muted"), children: truncateCells(suggestion.detail, Math.max(24, width - 6)) }) }), suggestion.command ? (_jsxs(Box, { marginLeft: 2, children: [_jsx(Text, { color: toneColor("muted"), children: "try " }), _jsx(Text, { color: toneColor("brand"), children: truncateCells(suggestion.command, Math.max(16, width - 10)) })] })) : null] }, `${suggestion.title}-${index}`)))] }));
}
export function contextSuggestionModel(suggestion) {
    return {
        severity: suggestion.severity,
        tone: toneForSeverity(suggestion.severity),
        title: suggestion.title,
        detail: suggestion.detail,
        savingsLabel: suggestion.savingsTokens && suggestion.savingsTokens > 0
            ? `-> save ~${formatContextSavingsTokens(suggestion.savingsTokens)}`
            : "",
        command: suggestion.command ?? "",
    };
}
export function formatContextSavingsTokens(tokens) {
    const rounded = Math.max(0, Math.round(tokens));
    if (rounded >= 10_000)
        return `${Math.round(rounded / 1000)}k tokens`;
    if (rounded >= 1000)
        return `${(rounded / 1000).toFixed(1).replace(/\.0$/, "")}k tokens`;
    return `${rounded} tokens`;
}
function toneForSeverity(severity) {
    if (severity === "success")
        return "success";
    if (severity === "warning")
        return "warning";
    if (severity === "error")
        return "error";
    return "brand";
}
