import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { ChevronRight } from 'lucide-react';
import { DiffView } from './DiffView';
import { formatDuration } from './utils';
import { ToolErrorDisplay } from './ToolErrorDisplay';
export function ApplyPatchRenderer({ contentJson, toolDurationMs, isExpanded, onToggle, }) {
    const artifact = contentJson.artifact;
    const timeStr = formatDuration(toolDurationMs);
    const formatSpan = (start, count) => {
        if (count <= 1)
            return `${start}`;
        return `${start}-${start + count - 1}`;
    };
    const formatHunkLabel = (hunk) => {
        const left = `-${formatSpan(hunk.oldStart, hunk.oldLines)}`;
        const right = `+${formatSpan(hunk.newStart, hunk.newLines)}`;
        const deltaParts = [];
        if (hunk.additions > 0)
            deltaParts.push(`+${hunk.additions}`);
        if (hunk.deletions > 0)
            deltaParts.push(`-${hunk.deletions}`);
        const delta = deltaParts.length > 0 ? ` (${deltaParts.join(', ')})` : '';
        return `${left} ${right}${delta}`;
    };
    const summary = artifact?.summary || {};
    const files = Number(summary.files || 0);
    const additions = Number(summary.additions || 0);
    const deletions = Number(summary.deletions || 0);
    const patch = artifact?.patch ? String(artifact.patch) : '';
    const changes = Array.isArray(contentJson.result?.changes)
        ? contentJson.result?.changes
        : [];
    const singleFilePath = files === 1 && changes.length > 0 ? changes[0].filePath : null;
    const hasError = contentJson.error ||
        (contentJson.result &&
            'ok' in contentJson.result &&
            contentJson.result.ok === false);
    const errorMessage = typeof contentJson.error === 'string'
        ? contentJson.error
        : contentJson.result &&
            'error' in contentJson.result &&
            typeof contentJson.result.error === 'string'
            ? contentJson.result.error
            : null;
    const errorStack = contentJson.result &&
        typeof contentJson.result === 'object' &&
        'stack' in contentJson.result &&
        typeof contentJson.result.stack === 'string'
        ? contentJson.result.stack
        : undefined;
    return (_jsxs("div", { className: "text-xs", children: [_jsxs("button", { type: "button", onClick: onToggle, className: `flex items-center gap-2 transition-colors w-full ${hasError
                    ? 'text-red-700 dark:text-red-300 hover:text-red-600 dark:hover:text-red-200'
                    : 'text-purple-700 dark:text-purple-300 hover:text-purple-600 dark:hover:text-purple-200'}`, children: [_jsx(ChevronRight, { className: `h-3 w-3 flex-shrink-0 transition-transform ${isExpanded ? 'rotate-90' : ''}` }), _jsxs("span", { className: "font-medium flex-shrink-0", children: ["apply patch", hasError ? ' error' : ''] }), _jsx("span", { className: "text-muted-foreground/70 flex-shrink-0", children: "\u00B7" }), singleFilePath ? (_jsx("span", { className: "text-foreground/70 min-w-0 flex-shrink overflow-hidden text-ellipsis whitespace-nowrap", dir: "rtl", title: singleFilePath, children: singleFilePath })) : (_jsxs("span", { className: "text-foreground/70 flex-shrink-0", children: [files, " ", files === 1 ? 'file' : 'files'] })), _jsxs("span", { className: "text-emerald-600 dark:text-emerald-400 flex-shrink-0", children: ["+", additions] }), _jsxs("span", { className: "text-red-600 dark:text-red-400 flex-shrink-0", children: ["-", deletions] }), _jsxs("span", { className: "text-muted-foreground/80 flex-shrink-0", children: ["\u00B7 ", timeStr] })] }), isExpanded && hasError && errorMessage && (_jsxs("div", { children: [_jsx(ToolErrorDisplay, { error: errorMessage, stack: errorStack, showStack: true }), patch && (_jsx("div", { className: "mt-2 ml-5", children: _jsxs("details", { children: [_jsx("summary", { className: "cursor-pointer text-xs text-red-700 dark:text-red-300 hover:text-red-600 dark:hover:text-red-200", children: "Show patch that failed" }), _jsx("div", { className: "mt-2", children: _jsx(DiffView, { patch: patch }) })] }) }))] })), isExpanded && !hasError && changes.length > 0 && (_jsx("div", { className: "mt-2 ml-5 space-y-2", children: changes.map((change) => (_jsxs("div", { className: "space-y-1", children: [_jsx("div", { className: "font-mono text-foreground/80", children: change.filePath }), _jsx("div", { className: "flex flex-wrap gap-2", children: change.hunks.map((hunk, index) => (_jsx("span", { className: "rounded bg-muted px-2 py-0.5 text-[0.65rem] font-mono text-muted-foreground", children: formatHunkLabel(hunk) }, `apply-patch-${change.filePath}-hunk-${index}`))) })] }, `apply-patch-${change.filePath}-${change.kind}`))) })), isExpanded && !hasError && patch && (_jsx("div", { className: "mt-2 ml-5", children: _jsx(DiffView, { patch: patch }) }))] }));
}
//# sourceMappingURL=ApplyPatchRenderer.js.map