import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { GitBranch } from 'lucide-react';
import { DiffView } from './DiffView';
import { formatDuration, isToolError, getErrorMessage } from './utils';
import { ToolErrorDisplay } from './ToolErrorDisplay';
import { ToolHeader, ToolHeaderSeparator, ToolHeaderDetail, ToolHeaderMeta, ToolHeaderSuccess, ToolHeaderError, ToolContentBox, } from './shared';
export function GitDiffRenderer({ contentJson, toolDurationMs, isExpanded, onToggle, }) {
    const result = contentJson.result || {};
    const args = contentJson.args || {};
    const patch = String(result.patch || result.diff || '');
    const all = args.all || result.all;
    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 lines = patch.split('\n');
    let files = 0;
    let additions = 0;
    let deletions = 0;
    for (const line of lines) {
        if (line.startsWith('diff --git'))
            files += 1;
        else if (line.startsWith('+') && !line.startsWith('+++'))
            additions += 1;
        else if (line.startsWith('-') && !line.startsWith('---'))
            deletions += 1;
    }
    const hasChanges = files > 0;
    return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: "git diff", isExpanded: isExpanded, onToggle: onToggle, isError: hasError, colorVariant: "purple", canExpand: true, children: [all && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsx("span", { className: "text-muted-foreground/60", children: "(all)" })] })), _jsx(ToolHeaderSeparator, {}), hasChanges ? (_jsxs(_Fragment, { children: [_jsxs(ToolHeaderDetail, { children: [files, " ", files === 1 ? 'file' : 'files'] }), additions > 0 && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsxs(ToolHeaderSuccess, { children: ["+", additions] })] })), deletions > 0 && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsxs(ToolHeaderError, { children: ["-", deletions] })] }))] })) : (_jsx("span", { className: "text-muted-foreground/60", children: "no changes" })), _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: "options", icon: _jsx(GitBranch, { className: "h-3 w-3" }), maxHeight: "", children: _jsx("div", { className: "px-3 py-2 font-mono text-xs bg-muted/10", children: all ? 'git diff (all working tree)' : 'git diff (staged only)' }) }), patch && _jsx(DiffView, { patch: patch }), !patch && (_jsx(ToolContentBox, { title: "diff", maxHeight: "", children: _jsx("div", { className: "px-3 py-2 text-muted-foreground/60 italic", children: "No changes to show" }) }))] }))] }));
}
//# sourceMappingURL=GitDiffRenderer.js.map