import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Globe, ExternalLink, Link } from 'lucide-react';
import { formatDuration, isToolError, getErrorMessage } from './utils';
import { ToolErrorDisplay } from './ToolErrorDisplay';
import { ToolHeader, ToolHeaderSeparator, ToolHeaderDetail, ToolHeaderMeta, ToolContentBox, } from './shared';
export function WebSearchRenderer({ contentJson, toolDurationMs, isExpanded, onToggle, }) {
    const result = contentJson.result || {};
    const args = (contentJson.args || {});
    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 isSearch = 'results' in result;
    const searchResults = result.results || [];
    const webContent = result;
    const query = typeof args.query === 'string' ? args.query : undefined;
    const url = typeof args.url === 'string' ? args.url : webContent.url;
    if (isSearch) {
        return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: "web search", isExpanded: isExpanded, onToggle: onToggle, isError: hasError, colorVariant: "purple", canExpand: true, children: [query && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsxs("span", { className: "font-mono text-foreground/90 text-[11px] truncate min-w-0", title: query, children: ["\"", query.length > 25 ? `${query.slice(0, 25)}…` : query, "\""] })] })), _jsx(ToolHeaderSeparator, {}), _jsxs(ToolHeaderDetail, { children: [searchResults.length, ' ', searchResults.length === 1 ? 'result' : 'results'] }), _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(Globe, { className: "h-3 w-3" }), copyText: query, maxHeight: "", children: _jsx("div", { className: "px-3 py-2 font-mono text-xs bg-muted/10 break-all", children: query || '(empty)' }) }), searchResults.length > 0 && (_jsx(ToolContentBox, { title: "results", copyText: searchResults
                                .map((r) => `${r.title}\n${r.url}`)
                                .join('\n\n'), maxHeight: "max-h-80", children: _jsx("div", { className: "divide-y divide-border/50", children: searchResults.map((item, i) => (_jsxs("div", { className: "px-3 py-2 hover:bg-muted/20", children: [_jsxs("a", { href: item.url, target: "_blank", rel: "noopener noreferrer", className: "flex items-center gap-1 text-blue-600 dark:text-blue-400 hover:underline font-medium", children: [item.title, _jsx(ExternalLink, { className: "h-3 w-3 flex-shrink-0" })] }), _jsx("p", { className: "text-muted-foreground text-xs mt-0.5", children: item.snippet }), _jsx("p", { className: "text-xs text-muted-foreground/60 truncate mt-0.5", children: item.url })] }, `${item.url}-${i}`))) }) })), searchResults.length === 0 && (_jsx(ToolContentBox, { title: "results", maxHeight: "", children: _jsx("div", { className: "px-3 py-2 text-muted-foreground/60 italic", children: "No results found" }) }))] }))] }));
    }
    const displayUrl = url && url.length > 50 ? `${url.slice(0, 50)}...` : url;
    return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: "web fetch", isExpanded: isExpanded, onToggle: onToggle, isError: hasError, colorVariant: "purple", canExpand: true, children: [displayUrl && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsx("span", { className: "text-foreground/70 truncate min-w-0", title: url, children: displayUrl })] })), _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: "url", icon: _jsx(Link, { className: "h-3 w-3" }), copyText: url, maxHeight: "", children: _jsx("div", { className: "px-3 py-2 font-mono text-xs bg-muted/10 break-all", children: _jsx("a", { href: url, target: "_blank", rel: "noopener noreferrer", className: "text-blue-600 dark:text-blue-400 hover:underline", children: url }) }) }), webContent.content && (_jsx(ToolContentBox, { title: "content", copyText: webContent.content, maxHeight: "max-h-80", children: _jsx("div", { className: "px-3 py-2 text-xs text-foreground/80 whitespace-pre-wrap break-words", children: webContent.content }) })), !webContent.content && (_jsx(ToolContentBox, { title: "content", maxHeight: "", children: _jsx("div", { className: "px-3 py-2 text-muted-foreground/60 italic", children: "No content returned" }) }))] }))] }));
}
//# sourceMappingURL=WebSearchRenderer.js.map