import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { GitCommit, CheckSquare, AlertTriangle } from 'lucide-react';
import { Button } from '../ui/Button';
import { GitFileItem } from './GitFileItem';
import { useGitStore } from '../../stores/gitStore';
import { useStageFiles, useUnstageFiles } from '../../hooks/useGit';
import { useFocusStore } from '../../stores/focusStore';
import { useEffect, useRef, useMemo } from 'react';
export function GitFileList({ status }) {
    const { openCommitModal, openDiff } = useGitStore();
    const stageFiles = useStageFiles();
    const unstageFiles = useUnstageFiles();
    const { currentFocus, gitFileIndex } = useFocusStore();
    const itemRefs = useRef(new Map());
    const hasConflicts = (status.conflicted?.length ?? 0) > 0;
    const hasStaged = status.staged.length > 0;
    const hasUnstaged = status.unstaged.length > 0 || status.untracked.length > 0;
    const unstagedFiles = [...status.unstaged, ...status.untracked];
    const hasUnstagedFiles = unstagedFiles.length > 0;
    const _allFiles = useMemo(() => {
        return [
            ...(status.conflicted ?? []),
            ...status.staged,
            ...status.unstaged,
            ...status.untracked,
        ];
    }, [status]);
    const unstagedPaths = new Set(status.unstaged.map((f) => f.path));
    const handleStageAll = () => {
        const filesToStage = unstagedFiles.map((f) => f.path);
        if (filesToStage.length > 0) {
            stageFiles.mutate(filesToStage);
        }
    };
    const _handleUnstageAll = () => {
        const filesToUnstage = status.staged.map((f) => f.path);
        if (filesToUnstage.length > 0) {
            unstageFiles.mutate(filesToUnstage);
        }
    };
    const conflictedLength = status.conflicted?.length ?? 0;
    useEffect(() => {
        if (currentFocus === 'git' && gitFileIndex >= 0) {
            const element = itemRefs.current.get(gitFileIndex);
            element?.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
            const allFiles = [
                ...(status.conflicted ?? []),
                ...status.staged,
                ...status.unstaged,
                ...status.untracked,
            ];
            const focusedFile = allFiles[gitFileIndex];
            if (focusedFile) {
                const isStaged = status.staged.includes(focusedFile);
                openDiff(focusedFile.path, isStaged);
            }
        }
    }, [
        currentFocus,
        gitFileIndex,
        status.conflicted,
        status.staged,
        status.unstaged,
        status.untracked,
        openDiff,
    ]);
    return (_jsxs("div", { className: "flex flex-col", children: [hasConflicts && (_jsxs("div", { className: "border-b border-border", children: [_jsx("div", { className: "px-4 py-2 bg-red-500/10 flex items-center justify-between", children: _jsxs("span", { className: "text-xs font-semibold text-red-500 uppercase flex items-center gap-1.5", children: [_jsx(AlertTriangle, { className: "w-3.5 h-3.5" }), "Conflicts (", conflictedLength, ")"] }) }), _jsx("div", { className: "divide-y divide-border", children: status.conflicted?.map((file, index) => {
                            const globalIndex = index;
                            const isFocused = currentFocus === 'git' && gitFileIndex === globalIndex;
                            return (_jsx("div", { ref: (el) => {
                                    if (el)
                                        itemRefs.current.set(globalIndex, el);
                                    else
                                        itemRefs.current.delete(globalIndex);
                                }, className: isFocused ? 'ring-1 ring-inset ring-primary/40' : '', children: _jsx(GitFileItem, { file: file, staged: false }) }, file.path));
                        }) })] })), hasStaged && (_jsxs("div", { className: "border-b border-border", children: [_jsxs("div", { className: "px-4 py-2 bg-muted/50 flex items-center justify-between", children: [_jsxs("span", { className: "text-xs font-semibold text-foreground uppercase", children: ["Staged Changes (", status.staged.length, ")"] }), _jsxs("div", { className: "flex items-center gap-1", children: [status.staged.length > 0 && !hasConflicts && (_jsxs(Button, { variant: "primary", size: "sm", onClick: openCommitModal, className: "h-6 text-xs", children: [_jsx(GitCommit, { className: "w-3 h-3 mr-1" }), "Commit"] })), status.staged.length > 0 && hasConflicts && (_jsx("span", { className: "text-xs text-muted-foreground", children: "Resolve conflicts first" }))] })] }), _jsx("div", { className: "divide-y divide-border", children: status.staged.map((file, index) => {
                            const globalIndex = conflictedLength + index;
                            const isFocused = currentFocus === 'git' && gitFileIndex === globalIndex;
                            return (_jsx("div", { ref: (el) => {
                                    if (el)
                                        itemRefs.current.set(globalIndex, el);
                                    else
                                        itemRefs.current.delete(globalIndex);
                                }, className: isFocused ? 'ring-1 ring-inset ring-primary/40' : '', children: _jsx(GitFileItem, { file: file, staged: true, showModifiedIndicator: unstagedPaths.has(file.path) }) }, file.path));
                        }) })] })), hasUnstaged && (_jsxs("div", { children: [_jsxs("div", { className: "px-4 py-2 bg-muted/50 flex items-center justify-between", children: [_jsxs("span", { className: "text-xs font-semibold text-foreground uppercase", children: ["Changes (", status.unstaged.length + status.untracked.length, ")"] }), hasUnstagedFiles && (_jsxs(Button, { variant: "ghost", size: "sm", onClick: handleStageAll, title: "Stage all changes", className: "h-6 text-xs", children: [_jsx(CheckSquare, { className: "w-3 h-3 mr-1" }), "Stage All"] }))] }), _jsxs("div", { className: "divide-y divide-border", children: [status.unstaged.map((file, index) => {
                                const globalIndex = conflictedLength + status.staged.length + index;
                                const isFocused = currentFocus === 'git' && gitFileIndex === globalIndex;
                                return (_jsx("div", { ref: (el) => {
                                        if (el)
                                            itemRefs.current.set(globalIndex, el);
                                        else
                                            itemRefs.current.delete(globalIndex);
                                    }, className: isFocused ? 'ring-1 ring-inset ring-primary/40' : '', children: _jsx(GitFileItem, { file: file, staged: false }) }, file.path));
                            }), status.untracked.map((file, index) => {
                                const globalIndex = conflictedLength +
                                    status.staged.length +
                                    status.unstaged.length +
                                    index;
                                const isFocused = currentFocus === 'git' && gitFileIndex === globalIndex;
                                return (_jsx("div", { ref: (el) => {
                                        if (el)
                                            itemRefs.current.set(globalIndex, el);
                                        else
                                            itemRefs.current.delete(globalIndex);
                                    }, className: isFocused ? 'ring-1 ring-inset ring-primary/40' : '', children: _jsx(GitFileItem, { file: file, staged: false }) }, file.path));
                            })] })] })), !hasConflicts && !hasStaged && !hasUnstaged && (_jsx("div", { className: "p-4 text-sm text-muted-foreground text-center", children: "No changes" }))] }));
}
//# sourceMappingURL=GitFileList.js.map