import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Search } from 'lucide-react';
import { formatDuration, isToolError, getErrorMessage } from './utils';
import { ToolErrorDisplay } from './ToolErrorDisplay';
import { ToolHeader, ToolHeaderSeparator, ToolHeaderDetail, ToolHeaderMeta, ToolContentBox, } from './shared';
export function SearchRenderer({ contentJson, toolDurationMs, isExpanded, onToggle, compact, }) {
    const result = contentJson.result || {};
    const args = (contentJson.args || {});
    const matches = result.matches || [];
    const files = result.files || [];
    const timeStr = formatDuration(toolDurationMs);
    const hasError = isToolError(result) || !!contentJson.error;
    const errorMessage = getErrorMessage(result) ||
        (typeof contentJson.error === 'string' ? contentJson.error : null);
    const errorStack = result && typeof result === 'object' && 'stack' in result
        ? String(result.stack)
        : undefined;
    const itemCount = matches.length > 0 ? matches.length : files.length;
    const itemLabel = matches.length > 0
        ? matches.length === 1
            ? 'match'
            : 'matches'
        : files.length === 1
            ? 'file'
            : 'files';
    const searchTerm = (() => {
        if (typeof args.pattern === 'string')
            return args.pattern;
        if (typeof args.query === 'string')
            return args.query;
        if (typeof args.filePattern === 'string')
            return args.filePattern;
        return '';
    })();
    const searchPath = (() => {
        if (typeof args.path === 'string' && args.path !== '.')
            return args.path;
        return undefined;
    })();
    return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: "search", isExpanded: isExpanded, onToggle: onToggle, isError: hasError, colorVariant: "amber", canExpand: true, children: [searchTerm && !compact && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsxs("span", { className: "font-mono text-foreground/90 text-[11px] truncate min-w-0", title: searchTerm, children: ["\"", searchTerm.length > 30
                                        ? `${searchTerm.slice(0, 30)}…`
                                        : searchTerm, "\""] })] })), !compact && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsxs(ToolHeaderDetail, { children: [itemCount, " ", itemLabel] }), _jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderMeta, { children: timeStr })] }))] }), isExpanded && hasError && errorMessage && (_jsx(ToolErrorDisplay, { error: errorMessage, stack: errorStack, showStack: true })), isExpanded && !hasError && (_jsxs("div", { className: "mt-2 ml-5 flex flex-col gap-2 max-w-full", children: [_jsx(ToolContentBox, { title: "query", icon: _jsx(Search, { className: "h-3 w-3" }), subtitle: searchPath, copyText: searchTerm, maxHeight: "", children: _jsx("div", { className: "px-3 py-2 font-mono text-xs bg-muted/10 break-all", children: searchTerm || '(empty)' }) }), matches.length > 0 && (_jsx(ToolContentBox, { title: "results", copyText: matches
                            .map((m) => {
                            const match = m;
                            return `${match.file}:${match.line}: ${match.text}`;
                        })
                            .join('\n'), maxHeight: "max-h-80", children: _jsx("div", { className: "divide-y divide-border/50", children: matches.map((match, i) => {
                                const m = match;
                                return (_jsxs("div", { className: "px-3 py-1.5 font-mono hover:bg-muted/20", children: [_jsxs("div", { className: "text-blue-600 dark:text-blue-400 truncate", children: [m.file, ":", m.line] }), _jsx("div", { className: "text-foreground/80 truncate", children: m.text })] }, `${m.file}-${m.line}-${i}`));
                            }) }) })), files.length > 0 && matches.length === 0 && (_jsx(ToolContentBox, { title: "files", copyText: files.map(String).join('\n'), maxHeight: "max-h-80", children: _jsx("div", { className: "divide-y divide-border/50", children: files.map((file) => (_jsx("div", { className: "px-3 py-1 font-mono text-foreground/80 truncate hover:bg-muted/20", children: String(file) }, String(file)))) }) })), itemCount === 0 && (_jsx(ToolContentBox, { title: "results", maxHeight: "", children: _jsx("div", { className: "px-3 py-2 text-muted-foreground/60 italic", children: "No results found" }) }))] }))] }));
}
//# sourceMappingURL=SearchRenderer.js.map