import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { prism, vscDarkPlus, } from 'react-syntax-highlighter/dist/esm/styles/prism';
const LANGUAGE_MAP = {
    js: 'javascript',
    jsx: 'jsx',
    ts: 'typescript',
    tsx: 'tsx',
    py: 'python',
    rb: 'ruby',
    go: 'go',
    rs: 'rust',
    java: 'java',
    c: 'c',
    cpp: 'cpp',
    h: 'c',
    hpp: 'cpp',
    cs: 'csharp',
    php: 'php',
    sh: 'bash',
    bash: 'bash',
    zsh: 'bash',
    sql: 'sql',
    json: 'json',
    yaml: 'yaml',
    yml: 'yaml',
    xml: 'xml',
    html: 'html',
    css: 'css',
    scss: 'scss',
    md: 'markdown',
    txt: 'plaintext',
    svelte: 'svelte',
};
function inferLanguageFromPath(path) {
    const extension = path.split('.').pop()?.toLowerCase() ?? '';
    if (!extension) {
        return 'plaintext';
    }
    return LANGUAGE_MAP[extension] ?? 'plaintext';
}
/**
 * Get just the filename from a path
 */
function getFileName(path) {
    const parts = path.split('/');
    return parts[parts.length - 1];
}
export function GitDiffViewer({ diff }) {
    const syntaxTheme = document?.documentElement.classList.contains('dark')
        ? vscDarkPlus
        : prism;
    const resolvedLanguage = diff.language && diff.language.trim().length > 0
        ? diff.language
        : inferLanguageFromPath(diff.file);
    // Get just the filename for the internal header
    const fileName = getFileName(diff.file);
    // Handle new files - show full content instead of diff
    if (diff.isNewFile && diff.content) {
        return (_jsxs("div", { className: "flex flex-col h-full bg-background", children: [_jsxs("div", { className: "px-4 py-2 bg-muted/50 flex items-center justify-between min-h-10", children: [_jsx("span", { className: "font-mono text-sm text-foreground truncate", title: diff.file, children: fileName }), _jsxs("div", { className: "flex items-center gap-3 text-xs flex-shrink-0", children: [_jsxs("span", { className: "text-green-600 dark:text-green-500", children: ["+", diff.insertions] }), _jsx("span", { className: "text-muted-foreground", children: resolvedLanguage })] })] }), _jsx("div", { className: "px-4 py-3 bg-green-500/10 border-b border-green-500/20", children: _jsxs("p", { className: "text-sm text-green-600 dark:text-green-400 font-medium", children: ["New file: ", diff.insertions, " lines"] }) }), _jsx("div", { className: "flex-1 overflow-auto", children: _jsx(SyntaxHighlighter, { language: resolvedLanguage, style: syntaxTheme, showLineNumbers: true, customStyle: {
                            margin: 0,
                            padding: '1rem',
                            background: 'transparent',
                        }, lineNumberStyle: {
                            minWidth: '3em',
                            paddingRight: '1em',
                            color: 'var(--color-muted-foreground)',
                            userSelect: 'none',
                        }, children: diff.content }) })] }));
    }
    // Handle binary files
    if (diff.isBinary) {
        return (_jsxs("div", { className: "flex flex-col h-full bg-background", children: [_jsxs("div", { className: "px-4 py-2 bg-muted/50 flex items-center justify-between min-h-10", children: [_jsx("span", { className: "font-mono text-sm text-foreground truncate", title: diff.file, children: fileName }), _jsx("div", { className: "flex items-center gap-3 text-xs flex-shrink-0", children: _jsx("span", { className: "text-muted-foreground", children: "Binary file" }) })] }), _jsx("div", { className: "flex-1 flex items-center justify-center", children: _jsx("div", { className: "p-4 text-center", children: _jsx("p", { className: "text-sm text-muted-foreground", children: "Binary file - cannot display diff" }) }) })] }));
    }
    // Parse the diff into lines with line numbers (for modified files)
    const lines = diff.diff.split('\n');
    const diffLines = [];
    let oldLineNum = 0;
    let newLineNum = 0;
    for (const line of lines) {
        if (line.startsWith('@@')) {
            // Parse hunk header to get starting line numbers
            const match = line.match(/@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
            if (match) {
                oldLineNum = parseInt(match[1], 10);
                newLineNum = parseInt(match[2], 10);
            }
            diffLines.push({
                oldLineNumber: null,
                newLineNumber: null,
                content: line,
                codeContent: line,
                type: 'hunk',
            });
        }
        else if (line.startsWith('diff') ||
            line.startsWith('index') ||
            line.startsWith('---') ||
            line.startsWith('+++')) {
            diffLines.push({
                oldLineNumber: null,
                newLineNumber: null,
                content: line,
                codeContent: line,
                type: 'meta',
            });
        }
        else if (line.startsWith('+')) {
            diffLines.push({
                oldLineNumber: null,
                newLineNumber: newLineNum,
                content: line,
                codeContent: line.slice(1), // Remove + prefix for syntax highlighting
                type: 'add',
            });
            newLineNum++;
        }
        else if (line.startsWith('-')) {
            diffLines.push({
                oldLineNumber: oldLineNum,
                newLineNumber: null,
                content: line,
                codeContent: line.slice(1), // Remove - prefix for syntax highlighting
                type: 'delete',
            });
            oldLineNum++;
        }
        else {
            // Context line
            diffLines.push({
                oldLineNumber: oldLineNum,
                newLineNumber: newLineNum,
                content: line,
                codeContent: line,
                type: 'context',
            });
            oldLineNum++;
            newLineNum++;
        }
    }
    // Render a single diff line with syntax highlighting
    const renderLine = (diffLine, index) => {
        let rowClassName = 'flex hover:bg-muted/20';
        let lineNumberClassName = 'flex-shrink-0 w-20 px-2 py-0.5 text-xs font-mono select-none border-r border-border';
        let contentClassName = 'flex-1 px-4 py-0.5 font-mono text-xs overflow-x-auto';
        // Apply background colors for add/delete/hunk
        if (diffLine.type === 'hunk') {
            rowClassName += ' bg-blue-500/10';
            lineNumberClassName += ' text-blue-600 dark:text-blue-400';
            contentClassName += ' text-blue-600 dark:text-blue-400 font-semibold';
        }
        else if (diffLine.type === 'add') {
            rowClassName += ' bg-green-500/10';
            lineNumberClassName += ' text-green-700 dark:text-green-400';
            contentClassName += ' text-green-700 dark:text-green-400';
        }
        else if (diffLine.type === 'delete') {
            rowClassName += ' bg-red-500/10';
            lineNumberClassName += ' text-red-600 dark:text-red-400';
            contentClassName += ' text-red-600 dark:text-red-400';
        }
        else if (diffLine.type === 'meta') {
            contentClassName += ' text-muted-foreground';
            lineNumberClassName += ' text-muted-foreground';
        }
        else {
            contentClassName += ' text-foreground/80';
            lineNumberClassName += ' text-muted-foreground';
        }
        const oldNum = diffLine.oldLineNumber !== null ? diffLine.oldLineNumber.toString() : '';
        const newNum = diffLine.newLineNumber !== null ? diffLine.newLineNumber.toString() : '';
        // For code lines (not meta/hunk), apply syntax highlighting
        let renderedContent = diffLine.content || ' ';
        if (diffLine.type !== 'meta' &&
            diffLine.type !== 'hunk' &&
            resolvedLanguage !== 'plaintext' &&
            diffLine.codeContent.trim()) {
            renderedContent = (_jsx(SyntaxHighlighter, { language: resolvedLanguage, style: syntaxTheme, customStyle: {
                    margin: 0,
                    padding: 0,
                    background: 'transparent',
                    display: 'inline',
                    fontSize: 'inherit',
                    lineHeight: 'inherit',
                }, codeTagProps: {
                    style: {
                        fontFamily: 'inherit',
                        background: 'transparent',
                    },
                }, PreTag: "span", children: diffLine.codeContent }));
            // Add back the +/- prefix if it was an add/delete
            if (diffLine.type === 'add') {
                renderedContent = (_jsxs(_Fragment, { children: [_jsx("span", { className: "select-none", children: "+" }), renderedContent] }));
            }
            else if (diffLine.type === 'delete') {
                renderedContent = (_jsxs(_Fragment, { children: [_jsx("span", { className: "select-none", children: "-" }), renderedContent] }));
            }
        }
        return (_jsxs("div", { className: rowClassName, children: [_jsx("div", { className: lineNumberClassName, children: _jsxs("div", { className: "flex justify-between gap-2", children: [_jsx("span", { className: "text-right w-8", children: oldNum }), _jsx("span", { className: "text-right w-8", children: newNum })] }) }), _jsx("div", { className: contentClassName, children: renderedContent })] }, index));
    };
    return (_jsxs("div", { className: "flex flex-col h-full bg-background", children: [_jsxs("div", { className: "px-4 py-2 bg-muted/50 flex items-center justify-between min-h-10", children: [_jsx("span", { className: "font-mono text-sm text-foreground truncate", title: diff.file, children: fileName }), _jsxs("div", { className: "flex items-center gap-3 text-xs flex-shrink-0", children: [diff.insertions > 0 && (_jsxs("span", { className: "text-green-600 dark:text-green-500", children: ["+", diff.insertions] })), diff.deletions > 0 && (_jsxs("span", { className: "text-red-600 dark:text-red-500", children: ["-", diff.deletions] })), _jsx("span", { className: "text-muted-foreground", children: resolvedLanguage })] })] }), _jsx("div", { className: "flex-1 overflow-auto", children: diff.diff.trim() === '' ? (_jsx("div", { className: "p-4 text-sm text-muted-foreground", children: "No changes to display" })) : (_jsx("div", { className: "min-w-max", children: diffLines.map(renderLine) })) })] }));
}
//# sourceMappingURL=GitDiffViewer.js.map