import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import React, { useState, useCallback, useMemo } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../../colors.js';
import { useKeypress } from '../../hooks/useKeypress.js';
export const ProfileAttachmentWizard = ({ subagent, profiles, getProfileInfo, onConfirm, onCancel, isFocused = true, }) => {
    const [selectedIndex, setSelectedIndex] = useState(() => {
        // Start at current profile if found
        const idx = profiles.indexOf(subagent.profile);
        return idx >= 0 ? idx : 0;
    });
    const [previewInfo, setPreviewInfo] = useState(null);
    const [isConfirming, setIsConfirming] = useState(false);
    const [error, setError] = useState(null);
    // Current selection
    const selectedProfile = profiles[selectedIndex] || '';
    // Load profile info when selection changes
    React.useEffect(() => {
        let cancelled = false;
        if (getProfileInfo && selectedProfile) {
            getProfileInfo(selectedProfile)
                .then((info) => {
                if (!cancelled)
                    setPreviewInfo(info);
            })
                .catch(() => {
                if (!cancelled)
                    setPreviewInfo(null);
            });
        }
        else {
            if (!cancelled)
                setPreviewInfo(null);
        }
        return () => {
            cancelled = true;
        };
    }, [selectedProfile, getProfileInfo]);
    const moveSelection = useCallback((delta) => {
        setSelectedIndex((prev) => {
            let newIndex = prev + delta;
            if (newIndex < 0)
                newIndex = 0;
            if (newIndex >= profiles.length)
                newIndex = profiles.length - 1;
            return newIndex;
        });
    }, [profiles.length]);
    const handleConfirm = useCallback(async () => {
        if (!selectedProfile)
            return;
        setIsConfirming(true);
        setError(null);
        try {
            await onConfirm(selectedProfile);
        }
        catch (err) {
            setError(err instanceof Error ? err.message : 'Failed to attach profile');
            setIsConfirming(false);
        }
    }, [selectedProfile, onConfirm]);
    useKeypress((key) => {
        if (isConfirming)
            return;
        if (key.name === 'escape') {
            onCancel();
            return;
        }
        if (key.name === 'up') {
            moveSelection(-1);
            return;
        }
        if (key.name === 'down') {
            moveSelection(1);
            return;
        }
        if (key.name === 'return') {
            // eslint-disable-next-line @typescript-eslint/no-floating-promises
            handleConfirm();
            return;
        }
    }, { isActive: isFocused && !isConfirming });
    // Calculate visible profiles (viewport)
    const maxVisible = 6;
    const startIndex = useMemo(() => Math.max(0, selectedIndex - Math.floor(maxVisible / 2)), [selectedIndex]);
    const endIndex = Math.min(profiles.length, startIndex + maxVisible);
    const visibleProfiles = profiles.slice(startIndex, endIndex);
    if (profiles.length === 0) {
        return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: Colors.Foreground, children: "No profiles available. Create a profile first." }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "[ESC] Cancel" }) })] }));
    }
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { bold: true, color: Colors.Foreground, children: ["Attach Profile to: ", subagent.name] }), _jsx(Text, { color: Colors.Gray, children: "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500" }), error && (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "#ff0000", children: error }) })), _jsx(Box, { marginY: 1, children: _jsxs(Text, { color: Colors.Foreground, children: ["Profile Selection (showing ", startIndex + 1, "-", endIndex, " of", ' ', profiles.length, "):"] }) }), _jsx(Box, { flexDirection: "column", marginBottom: 1, children: visibleProfiles.map((profile, idx) => {
                    const actualIndex = startIndex + idx;
                    const isSelected = actualIndex === selectedIndex;
                    const isCurrent = profile === subagent.profile;
                    return (_jsxs(Box, { children: [_jsxs(Text, { color: isSelected ? '#00ff00' : Colors.Foreground, children: [isSelected ? '→ ' : '  ', profile] }), isCurrent && _jsx(Text, { color: Colors.Gray, children: " (current)" })] }, profile));
                }) }), _jsxs(Box, { flexDirection: "column", marginY: 1, children: [_jsx(Text, { bold: true, color: Colors.Foreground, children: "Selected Profile Preview:" }), _jsx(Text, { color: Colors.Gray, children: "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500" }), previewInfo ? (_jsxs(_Fragment, { children: [previewInfo.provider && (_jsxs(Box, { children: [_jsx(Text, { color: Colors.Gray, children: "Provider: " }), _jsx(Text, { color: Colors.Foreground, children: previewInfo.provider })] })), previewInfo.model && (_jsxs(Box, { children: [_jsx(Text, { color: Colors.Gray, children: "Model: " }), _jsx(Text, { color: Colors.Foreground, children: previewInfo.model })] })), previewInfo.temperature !== undefined && (_jsxs(Box, { children: [_jsx(Text, { color: Colors.Gray, children: "Temperature: " }), _jsx(Text, { color: Colors.Foreground, children: previewInfo.temperature })] })), previewInfo.maxTokens !== undefined && (_jsxs(Box, { children: [_jsx(Text, { color: Colors.Gray, children: "Max Tokens: " }), _jsx(Text, { color: Colors.Foreground, children: previewInfo.maxTokens })] }))] })) : (_jsxs(Text, { color: Colors.Gray, children: ["Profile: ", selectedProfile] }))] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "Controls: \u2191\u2193 Navigate [Enter] Confirm [ESC] Cancel" }) }), isConfirming && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "Attaching profile..." }) }))] }));
};
//# sourceMappingURL=ProfileAttachmentWizard.js.map