import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useMemo } from 'react';
import { ChevronRight, FilePen, FileCode, FilePlus, FileEdit, RefreshCw, } from 'lucide-react';
import { useSessionFilesStore } from '../../stores/sessionFilesStore';
import { useSessionFiles } from '../../hooks/useSessionFiles';
import { Button } from '../ui/Button';
function getOperationIcon(operation) {
    switch (operation) {
        case 'write':
            return _jsx(FilePlus, { className: "w-3 h-3 text-green-500" });
        case 'patch':
            return _jsx(FileEdit, { className: "w-3 h-3 text-blue-500" });
        case 'edit':
            return _jsx(FileCode, { className: "w-3 h-3 text-yellow-500" });
        default:
            return _jsx(FilePen, { className: "w-3 h-3 text-muted-foreground" });
    }
}
function getFileName(path) {
    return path.split('/').pop() || path;
}
function countLinesFromPatch(op) {
    if (op.artifact?.summary) {
        return {
            additions: op.artifact.summary.additions,
            deletions: op.artifact.summary.deletions,
        };
    }
    const patch = op.artifact?.patch || op.patch;
    if (patch) {
        let additions = 0;
        let deletions = 0;
        const lines = patch.split('\n');
        for (const line of lines) {
            if (line.startsWith('+') && !line.startsWith('+++')) {
                additions++;
            }
            else if (line.startsWith('-') && !line.startsWith('---')) {
                deletions++;
            }
        }
        return { additions, deletions };
    }
    if (op.content) {
        const lineCount = op.content.split('\n').length;
        return { additions: lineCount, deletions: 0 };
    }
    return { additions: 0, deletions: 0 };
}
function SessionFileItem({ file }) {
    const openDiff = useSessionFilesStore((state) => state.openDiff);
    const selectedFile = useSessionFilesStore((state) => state.selectedFile);
    const isSelected = selectedFile === file.path;
    const lastOp = file.operations[file.operations.length - 1];
    const { totalAdditions, totalDeletions } = useMemo(() => {
        let additions = 0;
        let deletions = 0;
        for (const op of file.operations) {
            const stats = countLinesFromPatch(op);
            additions += stats.additions;
            deletions += stats.deletions;
        }
        return { totalAdditions: additions, totalDeletions: deletions };
    }, [file.operations]);
    const handleClick = () => {
        if (file.operations.length > 0) {
            openDiff(file.path, file.operations);
        }
    };
    return (_jsx("button", { type: "button", onClick: handleClick, className: `w-full text-left px-3 py-2 hover:bg-muted/50 transition-colors ${isSelected ? 'bg-muted' : ''}`, children: _jsxs("div", { className: "flex items-center gap-2", children: [getOperationIcon(lastOp?.operation || 'write'), _jsx("div", { className: "flex-1 min-w-0", children: _jsx("div", { className: "text-sm font-medium truncate", children: getFileName(file.path) }) }), _jsxs("div", { className: "flex items-center gap-1 text-xs shrink-0", children: [file.operationCount > 1 && (_jsxs("span", { className: "bg-muted text-muted-foreground px-1 rounded mr-1", children: [file.operationCount, "x"] })), _jsxs("span", { className: "text-green-500", children: ["+", totalAdditions] }), _jsxs("span", { className: "text-red-500", children: ["-", totalDeletions] })] })] }) }));
}
export const SessionFilesSidebar = memo(function SessionFilesSidebar({ sessionId, }) {
    const isExpanded = useSessionFilesStore((state) => state.isExpanded);
    const collapseSidebar = useSessionFilesStore((state) => state.collapseSidebar);
    const { data, isLoading, error, refetch } = useSessionFiles(sessionId);
    if (!isExpanded)
        return null;
    return (_jsxs("div", { className: "w-80 border-l border-border bg-background flex flex-col h-full", children: [_jsxs("div", { className: "h-14 border-b border-border px-4 flex items-center justify-between shrink-0", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx(FilePen, { className: "w-4 h-4 text-muted-foreground" }), _jsx("span", { className: "font-medium", children: "Session Files" }), data && data.totalFiles > 0 && (_jsxs("span", { className: "text-xs text-muted-foreground", children: ["(", data.totalFiles, ")"] }))] }), _jsx(Button, { variant: "ghost", size: "icon", onClick: collapseSidebar, children: _jsx(ChevronRight, { className: "w-4 h-4" }) })] }), _jsx("div", { className: "flex-1 overflow-y-auto", children: isLoading ? (_jsx("div", { className: "p-4 text-sm text-muted-foreground", children: "Loading session files..." })) : error ? (_jsx("div", { className: "p-4 text-sm text-orange-500", children: error instanceof Error ? error.message : 'Failed to load files' })) : !data || data.totalFiles === 0 ? (_jsx("div", { className: "p-4 text-sm text-muted-foreground", children: "No files modified in this session" })) : (_jsx("div", { className: "divide-y divide-border", children: data.files.map((file) => (_jsx(SessionFileItem, { file: file }, file.path))) })) }), _jsxs("div", { className: "h-12 px-4 border-t border-border text-xs text-muted-foreground flex items-center justify-between gap-2", children: [_jsxs("div", { className: "flex items-center gap-2 min-w-0 flex-1", children: [_jsx(FilePen, { className: "w-3 h-3 flex-shrink-0" }), data && data.totalOperations > 0 ? (_jsxs("span", { className: "truncate", children: [data.totalFiles, " file", data.totalFiles !== 1 ? 's' : '', " \u2022", ' ', data.totalOperations, " op", data.totalOperations !== 1 ? 's' : ''] })) : (_jsx("span", { className: "truncate", children: "No changes" }))] }), _jsx("div", { className: "flex items-center gap-1 flex-shrink-0", children: _jsx(Button, { variant: "ghost", size: "icon", onClick: () => refetch(), title: "Refresh session files", className: "h-6 w-6 transition-transform duration-200 hover:scale-110", disabled: isLoading, children: _jsx(RefreshCw, { className: `w-3 h-3 ${isLoading ? 'animate-spin' : ''}` }) }) })] })] }));
});
//# sourceMappingURL=SessionFilesSidebar.js.map