import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { parseActionSummary } from "./ActionSummaryBlock.js";
import { truncateCells } from "./design/textLayout.js";
export function McpApprovalPreviewBlock(props) {
    const model = mcpApprovalPreviewModel(props.summary);
    if (!model)
        return null;
    const width = props.width ?? 96;
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: model.risk === "high" ? "yellow" : "cyan", children: model.title }), " ", _jsx(Text, { color: "gray", children: model.risk })] }), _jsx(PreviewRow, { label: "server", value: model.server, width: width }), _jsx(PreviewRow, { label: "tool", value: model.tool, width: width }), _jsx(PreviewRow, { label: "args", value: model.argumentSummary, width: width }), _jsx(PreviewRow, { label: "timeout", value: model.timeout, width: width }), model.fingerprint ? _jsx(PreviewRow, { label: "sha", value: model.fingerprint, width: width }) : null, _jsx(PreviewRow, { label: "note", value: model.note, width: width, color: "gray" })] }));
}
export function mcpApprovalPreviewModel(summary) {
    const parsed = parseActionSummary(summary);
    if (!parsed || parsed.action !== "mcp_call")
        return null;
    const fields = Object.fromEntries(parsed.fields.map((field) => [field.key, field.value]));
    const server = fields.server || "(unknown server)";
    const tool = fields.tool || "(unknown tool)";
    const argumentCount = Number.parseInt(fields.argumentKeys || "0", 10);
    const hasArguments = Number.isFinite(argumentCount) && argumentCount > 0;
    const timeoutMs = fields.timeoutMs || "10000";
    return {
        action: "mcp_call",
        title: "MCP tool call",
        server,
        tool,
        argumentSummary: hasArguments ? `${argumentCount} key(s)` : "no arguments",
        timeout: `${timeoutMs} ms`,
        fingerprint: fields.sha || "",
        risk: hasArguments ? "high" : "medium",
        note: "MCP tools can perform server-defined side effects; approve only the expected server and tool",
    };
}
function PreviewRow(props) {
    return (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: "gray", children: [props.label.padEnd(9), " "] }), _jsx(Text, { color: props.color ?? "gray", children: truncateCells(props.value, Math.max(16, props.width - 12)) })] }));
}
