import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Plug } from 'lucide-react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { prism, vscDarkPlus, } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { formatDuration } from './utils';
import { ToolErrorDisplay } from './ToolErrorDisplay';
import { ToolHeader, ToolHeaderSeparator, ToolHeaderMeta, ToolHeaderSuccess, ToolHeaderError, ToolContentBox, ImagePreview, } from './shared';
function parseMcpToolName(fullName) {
    const idx = fullName.indexOf('__');
    if (idx === -1)
        return { server: '', tool: fullName };
    return { server: fullName.slice(0, idx), tool: fullName.slice(idx + 2) };
}
export function isMcpTool(name) {
    return name.includes('__');
}
export function McpToolRenderer({ toolName, contentJson, toolDurationMs, isExpanded, onToggle, compact, }) {
    const { server, tool } = parseMcpToolName(toolName);
    const result = contentJson.result || {};
    const args = contentJson.args || {};
    const timeStr = formatDuration(toolDurationMs);
    const syntaxTheme = document?.documentElement.classList.contains('dark')
        ? vscDarkPlus
        : prism;
    const hasToolError = typeof result === 'object' && 'ok' in result && result.ok === false;
    const errorMessage = hasToolError && 'error' in result && typeof result.error === 'string'
        ? result.error
        : typeof contentJson.error === 'string'
            ? contentJson.error
            : null;
    const errorStack = hasToolError && 'stack' in result && typeof result.stack === 'string'
        ? result.stack
        : undefined;
    const resultContent = typeof result.result === 'string'
        ? result.result
        : result.result && typeof result.result === 'object'
            ? JSON.stringify(result.result, null, 2)
            : null;
    const images = Array.isArray(result.images)
        ? result.images
        : [];
    const hasImages = images.length > 0;
    const hasContent = !hasToolError &&
        (resultContent || Object.keys(result).length > 0 || hasImages);
    const displayResult = resultContent || JSON.stringify(result, null, 2);
    const isJsonResult = displayResult.startsWith('{') || displayResult.startsWith('[');
    const argsPreview = Object.entries(args)
        .slice(0, 3)
        .map(([k, v]) => {
        const val = typeof v === 'string'
            ? v.length > 30
                ? `${v.slice(0, 27)}…`
                : v
            : JSON.stringify(v);
        return `${k}=${val}`;
    })
        .join('  ');
    const headerName = server ? `${server} › ${tool}` : tool;
    return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: headerName, isExpanded: isExpanded, onToggle: onToggle, isError: !!hasToolError, colorVariant: "purple", canExpand: !!hasContent || !!hasToolError || hasImages, children: [!compact && argsPreview && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsx("span", { className: "text-foreground/60 truncate max-w-xs font-mono text-[11px]", children: argsPreview })] })), !hasToolError && !compact && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderSuccess, { children: "ok" }), _jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderMeta, { children: timeStr })] })), hasToolError && !compact && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderError, { children: "error" }), _jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderMeta, { children: timeStr })] }))] }), isExpanded && hasToolError && errorMessage && (_jsx(ToolErrorDisplay, { error: errorMessage, stack: errorStack, showStack: true })), isExpanded && !hasToolError && hasContent && (_jsxs("div", { className: "mt-2 ml-5 flex flex-col gap-2 max-w-full", children: [hasImages && _jsx(ImagePreview, { images: images }), displayResult && !hasImages && (_jsx(ToolContentBox, { title: "result", icon: _jsx(Plug, { className: "h-3 w-3" }), copyText: displayResult, maxHeight: "max-h-[30rem]", children: _jsx(SyntaxHighlighter, { language: isJsonResult ? 'json' : 'text', style: syntaxTheme, customStyle: {
                                margin: 0,
                                padding: '0.75rem',
                                fontSize: '0.75rem',
                                lineHeight: '1.5',
                                background: 'transparent',
                                maxWidth: '100%',
                            }, wrapLines: true, wrapLongLines: true, children: displayResult }) }))] }))] }));
}
//# sourceMappingURL=McpToolRenderer.js.map