import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Box, Text } from "ink";
import { StatusBadge } from "../design/StatusBadge.js";
import { truncateCells } from "../design/textLayout.js";
import { displayToolName } from "./MCPToolListView.js";
export function MCPToolDetailView(props) {
    const model = mcpToolDetailModel(props.serverName, props.tool);
    const width = props.width ?? 96;
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { bold: true, color: "cyan", children: model.title }), model.badges.map((badge) => (_jsxs(React.Fragment, { children: [_jsx(Text, { children: " " }), _jsx(StatusBadge, { label: badge, tone: badge === "destructive" ? "error" : "success" })] }, badge)))] }), _jsx(Text, { color: "gray", children: truncateCells(model.fullName, width) }), model.description ? _jsx(Text, { children: truncateCells(model.description, width) }) : null, model.parameters.length > 0 ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { bold: true, children: "Parameters" }), model.parameters.map((parameter) => (_jsx(Text, { color: "gray", children: truncateCells(`${parameter.name}${parameter.required ? " *" : ""}: ${parameter.type} ${parameter.description}`, width) }, parameter.name)))] })) : (_jsx(Text, { color: "gray", children: "No parameters" }))] }));
}
export function mcpToolDetailModel(serverName, tool) {
    return {
        title: displayToolName(tool.name, serverName),
        fullName: tool.name,
        description: tool.description ?? "",
        badges: [
            tool.readOnly ? "read-only" : "",
            tool.destructive ? "destructive" : "",
            tool.openWorld ? "open-world" : "",
        ].filter(Boolean),
        parameters: schemaParameters(tool.inputSchema ?? tool.input_schema),
    };
}
export function schemaParameters(schema) {
    if (!schema || typeof schema !== "object" || Array.isArray(schema))
        return [];
    const record = schema;
    const properties = record.properties;
    if (!properties || typeof properties !== "object" || Array.isArray(properties))
        return [];
    const required = Array.isArray(record.required) ? new Set(record.required.map(String)) : new Set();
    return Object.entries(properties).map(([name, value]) => {
        const prop = value && typeof value === "object" && !Array.isArray(value)
            ? value
            : {};
        return {
            name,
            type: typeof prop.type === "string" ? prop.type : "unknown",
            required: required.has(name),
            description: typeof prop.description === "string" ? prop.description : "",
        };
    });
}
