import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useRef, memo, useState } from 'react';
import { X, Maximize2, Minimize2 } from 'lucide-react';
import { useGitStore } from '../../stores/gitStore';
import { useSidebarStore } from '../../stores/sidebarStore';
import { useGitDiff } from '../../hooks/useGit';
import { useGitDiffFullFile } from '../../hooks/useFileBrowser';
import { Button } from '../ui/Button';
import { GitDiffViewer } from './GitDiffViewer';
export const GitDiffPanel = memo(function GitDiffPanel() {
    // Use selectors to only subscribe to needed state
    const isDiffOpen = useGitStore((state) => state.isDiffOpen);
    const selectedFile = useGitStore((state) => state.selectedFile);
    const selectedFileStaged = useGitStore((state) => state.selectedFileStaged);
    const closeDiff = useGitStore((state) => state.closeDiff);
    const setCollapsed = useSidebarStore((state) => state.setCollapsed);
    const wasCollapsedRef = useRef(null);
    const prevDiffOpenRef = useRef(false);
    const { data: diff, isLoading } = useGitDiff(selectedFile, selectedFileStaged);
    const [showFullFile, setShowFullFile] = useState(false);
    const { data: fullFileDiff, isLoading: fullFileLoading } = useGitDiffFullFile(selectedFile, selectedFileStaged, showFullFile);
    const activeDiff = showFullFile && fullFileDiff ? fullFileDiff : diff;
    const activeLoading = showFullFile ? fullFileLoading : isLoading;
    useEffect(() => {
        if (!isDiffOpen)
            setShowFullFile(false);
        if (isDiffOpen && !prevDiffOpenRef.current) {
            // Diff just opened - save current state and collapse
            const { isCollapsed } = useSidebarStore.getState();
            wasCollapsedRef.current = isCollapsed;
            setCollapsed(true);
        }
        else if (!isDiffOpen && prevDiffOpenRef.current) {
            // Diff just closed - restore previous state
            if (wasCollapsedRef.current !== null) {
                setCollapsed(wasCollapsedRef.current);
                wasCollapsedRef.current = null;
            }
        }
        prevDiffOpenRef.current = isDiffOpen;
    }, [isDiffOpen, setCollapsed]);
    // Handle ESC key
    useEffect(() => {
        const handleEscape = (e) => {
            const target = e.target;
            const isInInput = target.tagName === 'INPUT' ||
                target.tagName === 'TEXTAREA' ||
                target.isContentEditable;
            if ((e.key === 'Escape' || (e.key === 'q' && !isInInput)) && isDiffOpen) {
                closeDiff();
            }
        };
        document.addEventListener('keydown', handleEscape);
        return () => document.removeEventListener('keydown', handleEscape);
    }, [isDiffOpen, closeDiff]);
    if (!isDiffOpen || !selectedFile)
        return null;
    return (_jsxs("div", { className: "absolute inset-0 bg-background z-50 flex flex-col animate-in slide-in-from-left duration-300", children: [_jsxs("div", { className: "h-14 border-b border-border px-4 flex items-center gap-3", children: [_jsx(Button, { variant: "ghost", size: "icon", onClick: closeDiff, title: "Close diff viewer (ESC)", children: _jsx(X, { className: "w-4 h-4" }) }), _jsxs("div", { className: "flex-1 flex items-center gap-2 min-w-0", children: [_jsx("span", { className: "text-sm font-medium text-foreground font-mono truncate", title: `${selectedFile}\n${activeDiff?.absPath || ''}`, children: selectedFile }), selectedFileStaged && (_jsx("span", { className: "text-xs px-2 py-0.5 rounded bg-primary/10 text-primary flex-shrink-0", children: "Staged" }))] }), _jsxs(Button, { variant: showFullFile ? 'secondary' : 'ghost', size: "sm", onClick: () => setShowFullFile((v) => !v), title: showFullFile ? 'Show diff only' : 'Show full file with diff', className: "flex items-center gap-1.5 text-xs h-7", children: [showFullFile ? (_jsx(Minimize2, { className: "w-3.5 h-3.5" })) : (_jsx(Maximize2, { className: "w-3.5 h-3.5" })), showFullFile ? 'Diff' : 'Full File'] })] }), _jsx("div", { className: "flex-1 overflow-auto", children: activeLoading ? (_jsx("div", { className: "h-full flex items-center justify-center text-muted-foreground", children: "Loading diff..." })) : activeDiff ? (_jsx(GitDiffViewer, { diff: activeDiff })) : (_jsx("div", { className: "h-full flex items-center justify-center text-muted-foreground", children: "No diff available" })) })] }));
});
//# sourceMappingURL=GitDiffPanel.js.map