import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { useTerminalSize } from "../hooks/useTerminalSize.js";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function McpPanel(props) {
    const { columns } = useTerminalSize();
    const width = Math.max(48, Math.min(108, columns - 4));
    return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsxs(Pane, { width: width, title: "mcp", tone: "brand", paddingX: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: "cyan", children: props.model.title }), _jsx(Text, { color: "gray", children: truncateCells(props.model.subtitle, Math.max(24, width - 20)) })] }), _jsx(StatusBadge, { label: `${props.model.rows.length}`, tone: props.model.rows.length > 0 ? "brand" : "muted" })] }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: props.model.rows.length === 0 ? (_jsx(Text, { color: "gray", children: "No MCP entries" })) : props.model.rows.map((row) => (_jsx(McpPanelRowView, { row: row, width: width }, row.key))) }), _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(24, width - 4)) })] }) }));
}
export function mcpServersPanelModel(servers) {
    return {
        title: "MCP servers",
        subtitle: `${servers.length} configured server${servers.length === 1 ? "" : "s"}`,
        rows: servers.map((server) => ({
            key: server.name,
            name: server.name,
            status: server.enabled ? "enabled" : "disabled",
            tone: server.enabled ? "success" : "muted",
            detail: `${server.type} ${server.command ?? server.url ?? ""}`.trim(),
            note: server.description || nextActionForServer(server),
        })),
        footer: "/mcp health | /mcp tools <name> | /mcp connect <name>",
    };
}
export function mcpValidationPanelModel(results) {
    return {
        title: "MCP validation",
        subtitle: `${results.length} validation result${results.length === 1 ? "" : "s"}`,
        rows: results.map((result) => ({
            key: result.name,
            name: result.name,
            status: result.ok ? "ok" : "failed",
            tone: result.ok ? "success" : "error",
            detail: result.ok ? "config valid" : result.errors.join("; "),
            note: result.warnings.length > 0 ? `warning: ${result.warnings.join("; ")}` : "",
        })),
        footer: "/mcp show <name> | /mcp enable <name> | /mcp disable <name>",
    };
}
export function mcpHealthPanelModel(results) {
    return {
        title: "MCP health",
        subtitle: `${results.length} probed server${results.length === 1 ? "" : "s"}`,
        rows: results.map((result) => ({
            key: result.name,
            name: result.name,
            status: result.status,
            tone: toneForHealth(result.status),
            detail: `${result.type} tools=${result.toolCount} latency=${result.latencyMs}ms attempts=${result.attempts}`,
            note: result.error || stderrNote(result.stderr),
        })),
        footer: "/mcp connect <name> keeps a session warm for repeated tool calls",
    };
}
export function mcpSessionsPanelModel(sessions) {
    return {
        title: "MCP sessions",
        subtitle: `${sessions.length} connected session${sessions.length === 1 ? "" : "s"}`,
        rows: sessions.map((session) => ({
            key: session.name,
            name: session.name,
            status: "connected",
            tone: "success",
            detail: `${session.type} tools=${session.toolCount} lastUsed=${new Date(session.lastUsedAtMs).toISOString()}`,
            note: `connected=${new Date(session.connectedAtMs).toISOString()}`,
        })),
        footer: "/mcp close <name> | /mcp close all",
    };
}
export function mcpToolsPanelModel(serverName, tools, stderr = "") {
    return {
        title: `MCP tools: ${serverName}`,
        subtitle: `${tools.length} available tool${tools.length === 1 ? "" : "s"}`,
        rows: tools.map((tool, index) => toolRow(serverName, tool, index)),
        footer: stderr.trim()
            ? `stderr: ${compact(stderr)}`
            : `/mcp call ${serverName} <tool> {"arg":"value"}`,
    };
}
function McpPanelRowView(props) {
    const detailWidth = Math.max(18, props.width - 28);
    const noteWidth = Math.max(18, props.width - 12);
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: props.row.status, tone: props.row.tone }), _jsx(Text, { children: " " }), _jsx(Text, { color: "cyan", children: truncateCells(props.row.name.padEnd(16), 16) }), _jsx(Text, { color: "gray", children: truncateCells(props.row.detail, detailWidth) })] }), props.row.note ? (_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "gray", children: "  " }), _jsx(Text, { color: "gray", children: truncateCells(props.row.note, noteWidth) })] })) : null] }));
}
function nextActionForServer(server) {
    if (!server.enabled)
        return `/mcp enable ${server.name}`;
    return server.type === "stdio"
        ? "stdio MCP requires shell permission for probe/call"
        : "http MCP can probe without shell permission";
}
function toneForHealth(status) {
    if (status === "ok")
        return "success";
    if (status === "disabled")
        return "muted";
    return "error";
}
function stderrNote(stderr) {
    const value = stderr?.trim() ?? "";
    return value ? `stderr: ${compact(value)}` : "";
}
function toolRow(serverName, value, index) {
    if (value && typeof value === "object" && !Array.isArray(value)) {
        const record = value;
        const name = String(record.name ?? `tool_${index + 1}`);
        const description = typeof record.description === "string" ? record.description : "";
        return {
            key: `${serverName}:${name}:${index}`,
            name,
            status: "tool",
            tone: "brand",
            detail: description || "no description",
            note: toolSchemaNote(record.inputSchema ?? record.input_schema),
        };
    }
    return {
        key: `${serverName}:tool:${index}`,
        name: `tool_${index + 1}`,
        status: "tool",
        tone: "brand",
        detail: compact(JSON.stringify(value)),
        note: "",
    };
}
function toolSchemaNote(value) {
    if (!value || typeof value !== "object" || Array.isArray(value))
        return "";
    const record = value;
    const properties = record.properties;
    if (!properties || typeof properties !== "object" || Array.isArray(properties))
        return "";
    const keys = Object.keys(properties);
    if (keys.length === 0)
        return "";
    return `params: ${keys.slice(0, 6).join(", ")}${keys.length > 6 ? ", ..." : ""}`;
}
function compact(value, maxChars = 160) {
    const singleLine = value.replace(/\s+/g, " ").trim();
    return singleLine.length > maxChars ? `${singleLine.slice(0, maxChars - 3)}...` : singleLine;
}
