import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { formatDuration, isToolError, getErrorMessage } from './utils';
import { ToolHeader, ToolHeaderSeparator, ToolHeaderMeta } from './shared';
import { ToolErrorDisplay } from './ToolErrorDisplay';
function parseStatusLine(line) {
    if (!line || line.length < 3)
        return null;
    const x = line[0];
    const y = line[1];
    const file = line.slice(3);
    if (x === '?' && y === '?') {
        return { status: '??', file, type: 'untracked' };
    }
    let type = 'unstaged';
    if (x !== ' ' && y !== ' ')
        type = 'both';
    else if (x !== ' ')
        type = 'staged';
    return { status: `${x}${y}`, file, type };
}
function getStatusIcon(status) {
    if (status === '??')
        return '?';
    if (status[0] === 'A' || status[1] === 'A')
        return '+';
    if (status[0] === 'M' || status[1] === 'M')
        return '~';
    if (status[0] === 'D' || status[1] === 'D')
        return '-';
    if (status[0] === 'R' || status[1] === 'R')
        return '→';
    return status;
}
function getStatusColor(type) {
    switch (type) {
        case 'staged':
            return 'text-emerald-600 dark:text-emerald-400';
        case 'unstaged':
            return 'text-amber-600 dark:text-amber-400';
        case 'untracked':
            return 'text-muted-foreground/60';
        case 'both':
            return 'text-blue-600 dark:text-blue-400';
    }
}
export function GitStatusRenderer({ contentJson, toolDurationMs, isExpanded, onToggle, }) {
    const result = contentJson.result || {};
    const hasError = isToolError(result);
    const errorMessage = getErrorMessage(result);
    const staged = Number(result.staged || 0);
    const unstaged = Number(result.unstaged || 0);
    const raw = (result.raw || []);
    const summary = String(result.summary || '');
    const timeStr = formatDuration(toolDurationMs);
    const statusLines = raw.map(parseStatusLine).filter(Boolean);
    const hasChanges = staged > 0 || unstaged > 0;
    const canExpand = statusLines.length > 0 || hasError || !!summary;
    return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: "git status", isExpanded: isExpanded, onToggle: onToggle, isError: hasError, colorVariant: "blue", canExpand: canExpand, children: [_jsx(ToolHeaderSeparator, {}), hasChanges ? (_jsxs(_Fragment, { children: [staged > 0 && (_jsxs("span", { className: "text-emerald-600 dark:text-emerald-400 flex-shrink-0", children: [staged, " staged"] })), unstaged > 0 && (_jsxs("span", { className: "text-amber-600 dark:text-amber-400 flex-shrink-0", children: [unstaged, " unstaged"] }))] })) : (_jsx("span", { className: "text-muted-foreground/60 flex-shrink-0", children: "clean" })), _jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderMeta, { children: timeStr })] }), isExpanded && hasError && errorMessage && (_jsx(ToolErrorDisplay, { error: errorMessage })), isExpanded && !hasError && (statusLines.length > 0 || summary) && (_jsx("div", { className: "mt-2 ml-5", children: statusLines.length > 0 ? (_jsx("div", { className: "bg-card/60 border border-border rounded-lg overflow-hidden max-h-96 overflow-y-auto", children: _jsx("div", { className: "divide-y divide-border/50", children: statusLines.map((line, i) => (_jsxs("div", { className: "flex items-center gap-2 px-3 py-1.5 font-mono hover:bg-muted/20", children: [_jsx("span", { className: `font-semibold w-6 flex-shrink-0 ${getStatusColor(line.type)}`, children: getStatusIcon(line.status) }), _jsx("span", { className: "text-foreground/80 break-all", children: line.file })] }, `${line.file}-${i}`))) }) })) : (summary && (_jsx("div", { className: "bg-card/60 border border-border rounded-lg p-3 text-muted-foreground", children: summary }))) }))] }));
}
//# sourceMappingURL=GitStatusRenderer.js.map