import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useRef, memo } from 'react';
import { X } from 'lucide-react';
import { useGitStore } from '../../stores/gitStore';
import { useSidebarStore } from '../../stores/sidebarStore';
import { useGitDiff } from '../../hooks/useGit';
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);
    useEffect(() => {
        // Only act on transitions
        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${diff?.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" }))] })] }), _jsx("div", { className: "flex-1 overflow-auto", children: isLoading ? (_jsx("div", { className: "h-full flex items-center justify-center text-muted-foreground", children: "Loading diff..." })) : diff ? (_jsx(GitDiffViewer, { diff: diff })) : (_jsx("div", { className: "h-full flex items-center justify-center text-muted-foreground", children: "No diff available" })) })] }));
});
//# sourceMappingURL=GitDiffPanel.js.map