import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useCallback, useState, useMemo } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
import { RadioButtonSelect, } from './shared/RadioButtonSelect.js';
import { useKeypress } from '../hooks/useKeypress.js';
import { usePermissionsModifyTrust } from '../hooks/usePermissionsModifyTrust.js';
import { TrustLevel } from '../../config/trustedFolders.js';
import { MessageType } from '../types.js';
export const PermissionsModifyTrustDialog = ({ onExit, addItem, onRestart }) => {
    const { currentTrustLevel, pendingTrustLevel, commitTrustLevel, isIdeTrusted, isParentTrusted, requiresRestart: _requiresRestart, workingDirectory, parentFolderName, } = usePermissionsModifyTrust();
    const [showRestartPrompt, setShowRestartPrompt] = useState(false);
    // Determine the display trust level text
    const getTrustLevelDisplay = useCallback((level) => {
        if (isIdeTrusted) {
            return 'Trusted (via IDE)';
        }
        if (isParentTrusted && !level) {
            return 'Trusted (via parent folder)';
        }
        switch (level) {
            case TrustLevel.TRUST_FOLDER:
                return 'Trusted';
            case TrustLevel.TRUST_PARENT:
                return 'Trust parent';
            case TrustLevel.DO_NOT_TRUST:
                return 'Not trusted';
            default:
                return 'Not set';
        }
    }, [isIdeTrusted, isParentTrusted]);
    const options = useMemo(() => [
        {
            label: 'Trust this folder',
            value: TrustLevel.TRUST_FOLDER,
            key: TrustLevel.TRUST_FOLDER,
        },
        {
            label: `Trust parent folder (${parentFolderName})`,
            value: TrustLevel.TRUST_PARENT,
            key: TrustLevel.TRUST_PARENT,
        },
        {
            label: "Don't trust",
            value: TrustLevel.DO_NOT_TRUST,
            key: TrustLevel.DO_NOT_TRUST,
        },
    ], [parentFolderName]);
    // Find initial index based on current trust level
    const initialIndex = useMemo(() => {
        if (!currentTrustLevel)
            return 0;
        const index = options.findIndex((o) => o.value === currentTrustLevel);
        return index >= 0 ? index : 0;
    }, [currentTrustLevel, options]);
    const handleSelect = useCallback((level) => {
        commitTrustLevel(level);
        // Check if we need to show restart prompt
        if (level !== currentTrustLevel) {
            addItem({
                type: MessageType.INFO,
                text: `Trust level for ${workingDirectory} set to ${getTrustLevelDisplay(level)}.`,
            }, Date.now());
            setShowRestartPrompt(true);
        }
        else {
            // No change needed, just exit
            addItem({
                type: MessageType.INFO,
                text: `Trust level unchanged for ${workingDirectory}`,
            }, Date.now());
            onExit();
        }
    }, [
        commitTrustLevel,
        currentTrustLevel,
        addItem,
        workingDirectory,
        onExit,
        getTrustLevelDisplay,
    ]);
    useKeypress((key) => {
        if (key.name === 'escape') {
            if (showRestartPrompt) {
                onExit();
            }
            else {
                onExit();
            }
        }
        if (key.name === 'r' && showRestartPrompt) {
            onRestart?.();
        }
    }, { isActive: true });
    // Generate warning message for inherited trust
    const warningMessage = useMemo(() => {
        if (isIdeTrusted) {
            return 'This folder is trusted via your IDE settings. Changes here will only take effect when not using the IDE.';
        }
        if (isParentTrusted) {
            return 'This folder is trusted via a parent folder setting. You can override it with a more specific rule.';
        }
        return null;
    }, [isIdeTrusted, isParentTrusted]);
    if (showRestartPrompt) {
        return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: Colors.AccentYellow, padding: 1, width: "100%", marginLeft: 1, children: [_jsx(Text, { bold: true, children: "Trust level updated" }), _jsxs(Text, { children: ["Trust level has been set to:", ' ', _jsx(Text, { color: Colors.AccentGreen, children: getTrustLevelDisplay(pendingTrustLevel) })] })] }), _jsx(Box, { marginLeft: 1, marginTop: 1, children: _jsx(Text, { color: Colors.AccentYellow, children: "To see changes, llxprt must be restarted. Press 'r' to exit and apply changes now, or Escape to continue without restart." }) })] }));
    }
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: Colors.Gray, padding: 1, width: "100%", marginLeft: 1, children: [_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { bold: true, children: "Modify Trust Settings" }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: ["Folder: ", _jsx(Text, { color: Colors.AccentBlue, children: workingDirectory })] }) }), _jsx(Box, { children: _jsxs(Text, { children: ["Current: ", getTrustLevelDisplay(currentTrustLevel)] }) })] }), warningMessage && (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: Colors.AccentYellow, children: warningMessage }) })), _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { children: "Select trust level:" }), _jsx(RadioButtonSelect, { items: options, initialIndex: initialIndex, onSelect: handleSelect, isFocused: true })] })] }), _jsx(Box, { marginLeft: 1, marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "(Use Enter to select, Escape to cancel)" }) })] }));
};
//# sourceMappingURL=PermissionsModifyTrustDialog.js.map