import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useMemo, useCallback } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../../colors.js';
import { useKeypress } from '../../hooks/useKeypress.js';
import { ListFocusMode } from './types.js';
export const SubagentListMenu = ({ subagents, onSelect, onEdit, onAttachProfile, onDelete, onBack, isLoading = false, isFocused = true, }) => {
    const [searchTerm, setSearchTerm] = useState('');
    const [focusMode, setFocusMode] = useState(ListFocusMode.LIST);
    const [selectedIndex, setSelectedIndex] = useState(0);
    // Filter subagents based on search term
    const filteredSubagents = useMemo(() => {
        if (!searchTerm.trim())
            return subagents;
        const term = searchTerm.toLowerCase();
        return subagents.filter((s) => s.name.toLowerCase().includes(term) ||
            s.profile.toLowerCase().includes(term));
    }, [subagents, searchTerm]);
    // Ensure selected index is valid after filtering
    const safeIndex = useMemo(() => {
        if (filteredSubagents.length === 0)
            return 0;
        return Math.min(selectedIndex, filteredSubagents.length - 1);
    }, [selectedIndex, filteredSubagents.length]);
    const moveSelection = useCallback((delta) => {
        if (filteredSubagents.length === 0)
            return;
        let newIndex = safeIndex + delta;
        if (newIndex < 0)
            newIndex = 0;
        if (newIndex >= filteredSubagents.length)
            newIndex = filteredSubagents.length - 1;
        setSelectedIndex(newIndex);
    }, [safeIndex, filteredSubagents.length]);
    const handleSearchInput = useCallback((char) => {
        setSearchTerm((prev) => prev + char);
    }, []);
    const handleBackspace = useCallback(() => {
        setSearchTerm((prev) => prev.slice(0, -1));
    }, []);
    useKeypress((key) => {
        const input = key.sequence;
        // ESC handling
        if (key.name === 'escape') {
            if (focusMode === ListFocusMode.SEARCH && searchTerm) {
                // Clear search
                setSearchTerm('');
                setFocusMode(ListFocusMode.LIST);
            }
            else if (focusMode === ListFocusMode.SEARCH) {
                // Exit search mode
                setFocusMode(ListFocusMode.LIST);
            }
            else {
                // Back to menu
                onBack();
            }
            return;
        }
        // Tab to activate search
        if (key.name === 'tab' && focusMode === ListFocusMode.LIST) {
            setFocusMode(ListFocusMode.SEARCH);
            return;
        }
        // Tab to switch focus
        if (key.name === 'tab' && focusMode === ListFocusMode.SEARCH) {
            setFocusMode(ListFocusMode.LIST);
            return;
        }
        // Search mode input handling
        if (focusMode === ListFocusMode.SEARCH) {
            if (key.name === 'backspace' || key.name === 'delete') {
                handleBackspace();
                return;
            }
            if (key.name === 'return') {
                setFocusMode(ListFocusMode.LIST);
                return;
            }
            // Type character
            if (input && input.length === 1 && !key.ctrl && !key.meta) {
                handleSearchInput(input);
            }
            return;
        }
        // List navigation mode
        if (focusMode === ListFocusMode.LIST) {
            if (key.name === 'up') {
                moveSelection(-1);
                return;
            }
            if (key.name === 'down') {
                moveSelection(1);
                return;
            }
            if (key.name === 'return' && filteredSubagents.length > 0) {
                onSelect(filteredSubagents[safeIndex]);
                return;
            }
            // Quick actions
            if (input === 'e' && filteredSubagents.length > 0) {
                onEdit(filteredSubagents[safeIndex]);
                return;
            }
            if (input === 'a' && filteredSubagents.length > 0) {
                onAttachProfile(filteredSubagents[safeIndex]);
                return;
            }
            if (input === 'd' && filteredSubagents.length > 0) {
                onDelete(filteredSubagents[safeIndex]);
                return;
            }
        }
    }, { isActive: isFocused && !isLoading });
    if (isLoading) {
        return (_jsx(Box, { flexDirection: "column", children: _jsx(Text, { color: Colors.Gray, children: "Loading subagents..." }) }));
    }
    if (subagents.length === 0) {
        return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: Colors.Foreground, children: "No subagents found. Use 'Create Subagent' to create one." }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "[ESC] Back to menu" }) })] }));
    }
    // Calculate visible items (viewport)
    const maxVisible = 10;
    const startIndex = Math.max(0, safeIndex - Math.floor(maxVisible / 2));
    const endIndex = Math.min(filteredSubagents.length, startIndex + maxVisible);
    const visibleSubagents = filteredSubagents.slice(startIndex, endIndex);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: Colors.Gray, children: "Search: " }), focusMode === ListFocusMode.SEARCH ? (_jsxs(Text, { color: Colors.Foreground, children: [searchTerm, _jsx(Text, { color: "#00ff00", children: "|" })] })) : (_jsx(Text, { color: Colors.Gray, children: searchTerm || '(press Tab to search)' })), _jsxs(Text, { color: Colors.Gray, children: [' ', "Found ", filteredSubagents.length, searchTerm ? ` of ${subagents.length}` : '', " subagents"] })] }), _jsx(Box, { flexDirection: "column", marginBottom: 1, children: filteredSubagents.length === 0 ? (_jsx(Text, { color: Colors.Gray, children: "No matching subagents" })) : (visibleSubagents.map((subagent, idx) => {
                    const actualIndex = startIndex + idx;
                    const isSelected = actualIndex === safeIndex && focusMode === ListFocusMode.LIST;
                    return (_jsxs(Box, { children: [_jsxs(Text, { color: isSelected ? '#00ff00' : Colors.Foreground, children: [isSelected ? '→ ' : '  ', subagent.name.padEnd(25)] }), _jsx(Text, { color: Colors.Gray, children: subagent.profile })] }, subagent.name));
                })) }), _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: Colors.Gray, children: "Controls: \u2191\u2193 Navigate [Enter] Details [e] Edit" }), _jsx(Text, { color: Colors.Gray, children: " [a] Attach [d] Delete [ESC] Main menu" })] })] }));
};
//# sourceMappingURL=SubagentListMenu.js.map