import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
import { useKeypress } from '../hooks/useKeypress.js';
export const LoadProfileDialog = ({ profiles, onSelect, onClose, isLoading = false, }) => {
    const [index, setIndex] = useState(0);
    const columns = 2;
    const longest = profiles.reduce((len, p) => Math.max(len, p.length), 0);
    const colWidth = Math.max(longest + 4, 30);
    const rows = Math.ceil(profiles.length / columns);
    const move = (delta) => {
        let next = index + delta;
        if (next < 0)
            next = 0;
        if (next >= profiles.length)
            next = profiles.length - 1;
        setIndex(next);
    };
    useKeypress((key) => {
        if (key.name === 'escape')
            return onClose();
        if (profiles.length === 0) {
            return;
        }
        if (key.name === 'return')
            return onSelect(profiles[index]);
        if (key.name === 'left')
            move(-1);
        if (key.name === 'right')
            move(1);
        if (key.name === 'up')
            move(-columns);
        if (key.name === 'down')
            move(columns);
    }, { isActive: !isLoading });
    const renderItem = (name, i) => {
        const selected = i === index;
        return (_jsx(Box, { width: colWidth, marginRight: 2, children: _jsxs(Text, { color: selected ? '#00ff00' : Colors.Foreground, children: [selected ? '● ' : '○ ', name] }) }, name));
    };
    const grid = [];
    for (let r = 0; r < rows; r++) {
        const rowItems = [];
        for (let c = 0; c < columns; c++) {
            const i = r * columns + c;
            if (i < profiles.length)
                rowItems.push(renderItem(profiles[i], i));
        }
        grid.push(_jsx(Box, { children: rowItems }, r));
    }
    if (isLoading) {
        return (_jsx(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", padding: 1, children: _jsx(Text, { color: Colors.Foreground, children: "Loading profiles..." }) }));
    }
    if (profiles.length === 0) {
        return (_jsx(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", padding: 1, children: _jsx(Text, { color: Colors.Foreground, children: "No saved profiles found. Use /save to create a profile." }) }));
    }
    return (_jsxs(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", padding: 1, children: [_jsx(Text, { bold: true, color: Colors.Foreground, children: "Select Profile (\u2190/\u2192/\u2191/\u2193, Enter to load, Esc to cancel)" }), grid] }));
};
//# sourceMappingURL=LoadProfileDialog.js.map