import { jsx as _jsx, jsxs as _jsxs } 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 { SemanticColors } from '../colors.js';
import { useResponsive } from '../hooks/useResponsive.js';
import { useKeypress } from '../hooks/useKeypress.js';
/**
 * Allowlist of ephemeralSettings keys that are safe to display.
 * Any key NOT in this set will be hidden to prevent accidental secret leakage.
 */
const SAFE_EPHEMERAL_KEYS = new Set([
    'baseurl',
    'endpoint',
    'url',
    'timeout',
    'maxretries',
    'retries',
    'region',
    'debug',
    'loglevel',
    'version',
    'apiversion',
    'organization',
    'orgid',
    'project',
    'projectid',
    'maxtokens',
    'temperature',
    'topp',
    'topk',
    'stream',
    'safetysettings',
]);
// Type guard for load balancer profile
function isLoadBalancerProfile(profile) {
    const p = profile;
    return (profile.type === 'loadbalancer' &&
        Array.isArray(p.profiles) &&
        typeof p.policy === 'string');
}
export const ProfileDetailDialog = ({ profileName, profile, onClose, onLoad, onDelete, onSetDefault, onEdit, isLoading = false, isDefault = false, isActive = false, error, }) => {
    const { isNarrow, width } = useResponsive();
    const [confirmDelete, setConfirmDelete] = useState(false);
    useKeypress((key) => {
        if (key.name === 'escape') {
            if (confirmDelete) {
                setConfirmDelete(false);
            }
            else {
                return onClose();
            }
        }
        // On error / missing profile screens, only Esc should do anything.
        if (error || !profile) {
            return;
        }
        if (confirmDelete) {
            if (key.sequence === 'y' || key.sequence === 'Y') {
                onDelete(profileName);
            }
            else if (key.sequence === 'n' || key.sequence === 'N') {
                setConfirmDelete(false);
            }
            return;
        }
        // Quick actions
        if (key.sequence === 'l') {
            return onLoad(profileName);
        }
        if (key.sequence === 'e') {
            return onEdit(profileName);
        }
        if (key.sequence === 'd') {
            setConfirmDelete(true);
        }
        if (key.sequence === 's') {
            return onSetDefault(profileName);
        }
    }, { isActive: !isLoading });
    if (isLoading) {
        return (_jsx(Box, { borderStyle: "round", borderColor: SemanticColors.border.default, flexDirection: "column", padding: 1, children: _jsx(Text, { color: SemanticColors.text.primary, children: "Loading profile..." }) }));
    }
    if (error) {
        return (_jsxs(Box, { borderStyle: "round", borderColor: SemanticColors.status.error, flexDirection: "column", padding: 1, children: [_jsx(Text, { bold: true, color: SemanticColors.status.error, children: "Error Loading Profile" }), _jsx(Text, { color: SemanticColors.text.secondary, children: error }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: SemanticColors.text.secondary, children: "Press Esc to go back" }) })] }));
    }
    if (!profile) {
        return (_jsxs(Box, { borderStyle: "round", borderColor: SemanticColors.border.default, flexDirection: "column", padding: 1, children: [_jsxs(Text, { color: SemanticColors.text.secondary, children: ["Profile not found: ", profileName] }), _jsx(Text, { color: SemanticColors.text.secondary, children: "Press Esc to go back" })] }));
    }
    // Render JSON config in a readable format
    const renderConfig = () => {
        if (isLoadBalancerProfile(profile)) {
            return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Type: " }), _jsx(Text, { color: SemanticColors.text.accent, children: "Load Balancer" })] }), _jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Policy: " }), _jsx(Text, { color: SemanticColors.text.primary, children: profile.policy })] }), _jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Member Profiles:" }), profile.profiles.map((p) => (_jsxs(Text, { color: SemanticColors.text.primary, children: ['  ', "- ", p] }, p)))] })] }));
        }
        // Standard profile
        return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Type: " }), _jsx(Text, { color: SemanticColors.text.primary, children: "Standard" })] }), _jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Provider: " }), _jsx(Text, { color: SemanticColors.text.accent, children: profile.provider })] }), _jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Model: " }), _jsx(Text, { color: SemanticColors.text.primary, children: profile.model })] }), profile.modelParams && Object.keys(profile.modelParams).length > 0 && (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Model Parameters:" }), Object.entries(profile.modelParams).map(([key, value]) => (_jsxs(Text, { color: SemanticColors.text.primary, children: ['  ', key, ": ", JSON.stringify(value)] }, key)))] })), profile.ephemeralSettings && (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Settings:" }), Object.entries(profile.ephemeralSettings)
                            .filter(([key]) => SAFE_EPHEMERAL_KEYS.has(key.toLowerCase()))
                            .filter(([, value]) => value !== undefined && value !== null)
                            .slice(0, 10) // Limit displayed settings
                            .map(([key, value]) => (_jsxs(Text, { color: SemanticColors.text.primary, children: ['  ', key, ": ", JSON.stringify(value)] }, key)))] })), profile.auth && (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: SemanticColors.text.secondary, children: "Authentication:" }), _jsxs(Text, { color: SemanticColors.text.primary, children: ['  ', "Type: ", profile.auth.type] }), profile.auth.buckets && profile.auth.buckets.length > 0 && (_jsxs(Text, { color: SemanticColors.text.primary, children: ['  ', "Buckets: ", profile.auth.buckets.join(', ')] }))] }))] }));
    };
    // Delete confirmation overlay
    if (confirmDelete) {
        return (_jsxs(Box, { borderStyle: "round", borderColor: SemanticColors.status.warning, flexDirection: "column", padding: 1, width: Math.min(width, 60), children: [_jsx(Text, { bold: true, color: SemanticColors.status.warning, children: "Delete Profile?" }), _jsx(Box, { marginY: 1, children: _jsxs(Text, { color: SemanticColors.text.primary, children: ["Are you sure you want to delete \"", profileName, "\"?"] }) }), _jsx(Text, { color: SemanticColors.text.secondary, children: "This action cannot be undone." }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: SemanticColors.text.accent, children: "Press y to confirm, n or Esc to cancel" }) })] }));
    }
    const dialogWidth = isNarrow ? undefined : Math.min(width, 80);
    return (_jsxs(Box, { borderStyle: "round", borderColor: SemanticColors.border.default, flexDirection: "column", padding: 1, width: dialogWidth, children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { bold: true, color: SemanticColors.text.accent, children: profileName }), isActive && (_jsx(Text, { color: SemanticColors.status.success, children: " (Active)" })), isDefault && (_jsx(Text, { color: SemanticColors.text.secondary, children: " (Default)" }))] }), renderConfig(), _jsx(Box, { marginTop: 1, borderStyle: "single", borderTop: true, borderBottom: false, borderLeft: false, borderRight: false, borderColor: SemanticColors.border.default, paddingTop: 1, children: _jsxs(Text, { color: SemanticColors.text.secondary, children: ["Actions: ", _jsx(Text, { color: SemanticColors.text.accent, children: "l" }), "=load", ' ', _jsx(Text, { color: SemanticColors.text.accent, children: "e" }), "=edit", ' ', _jsx(Text, { color: SemanticColors.text.accent, children: "d" }), "=delete", ' ', _jsx(Text, { color: SemanticColors.text.accent, children: "s" }), "=set-default", ' ', _jsx(Text, { color: SemanticColors.text.secondary, children: "Esc" }), "=back"] }) })] }));
};
//# sourceMappingURL=ProfileDetailDialog.js.map