import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { execSync } from 'child_process';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { Box, Text } from 'ink';
import { dirname, join } from 'path';
import React from 'react';
import { TitledBoxWithPreferences } from '../components/ui/titled-box.js';
import { confDirMap } from '../config/index.js';
import { getConfigPath } from '../config/paths.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { formatError } from '../utils/error-formatter.js';
function getConfigOptions() {
    const globalDir = getConfigPath();
    const cwdDir = process.cwd();
    const options = [];
    // agents.config.json — show project-level if it exists, always show global
    const projectAgentsConfig = join(cwdDir, 'agents.config.json');
    const globalAgentsConfig = join(globalDir, 'agents.config.json');
    if (existsSync(projectAgentsConfig)) {
        options.push({
            label: 'agents.config.json (project)',
            path: projectAgentsConfig,
            description: 'Project-level provider, tool, and session config',
        });
    }
    options.push({
        label: 'agents.config.json (global)',
        path: globalAgentsConfig,
        description: 'Global provider, tool, and session config',
    });
    // .mcp.json — project and global
    const projectMcp = join(cwdDir, '.mcp.json');
    const globalMcp = join(globalDir, '.mcp.json');
    if (existsSync(projectMcp)) {
        options.push({
            label: '.mcp.json (project)',
            path: projectMcp,
            description: 'Project-level MCP server configuration',
        });
    }
    options.push({
        label: '.mcp.json (global)',
        path: globalMcp,
        description: 'Global MCP server configuration',
    });
    // Preferences
    const loadedPreferences = confDirMap['nanocoder-preferences.json'];
    if (loadedPreferences) {
        options.push({
            label: 'nanocoder-preferences.json',
            path: loadedPreferences,
            description: 'User preferences (theme, shapes, last model)',
        });
    }
    return options;
}
function getEditor() {
    if (process.env.EDITOR)
        return process.env.EDITOR;
    if (process.env.VISUAL)
        return process.env.VISUAL;
    // When running inside VS Code's integrated terminal, default to code
    if (process.env.TERM_PROGRAM === 'vscode')
        return 'code --wait';
    return 'vi';
}
function ensureFileExists(filePath) {
    const dir = dirname(filePath);
    if (!existsSync(dir)) {
        mkdirSync(dir, { recursive: true });
    }
    if (!existsSync(filePath)) {
        if (filePath.endsWith('.json')) {
            writeFileSync(filePath, '{}\n', 'utf-8');
        }
        else {
            writeFileSync(filePath, '', 'utf-8');
        }
    }
}
function openInEditor(filePath) {
    const editor = getEditor();
    ensureFileExists(filePath);
    // nosemgrep: javascript.lang.security.detect-child-process.detect-child-process
    execSync(`${editor} ${JSON.stringify(filePath)}`, { stdio: 'inherit' });
}
function SetupConfigResult({ options, selectedIndex, error, }) {
    const boxWidth = useTerminalWidth();
    const { colors } = useTheme();
    const editor = getEditor();
    if (error) {
        return (_jsx(Box, { flexDirection: "column", children: _jsx(Text, { color: colors.error, children: error }) }));
    }
    if (selectedIndex !== undefined) {
        const option = options[selectedIndex];
        if (!option) {
            return (_jsx(Box, { children: _jsx(Text, { color: colors.error, children: "Invalid selection" }) }));
        }
        return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { color: colors.success, children: ["Opened ", option.label, " in ", editor] }), _jsx(Text, { color: colors.secondary, children: option.path })] }));
    }
    return (_jsxs(TitledBoxWithPreferences, { title: "Configuration Files", width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", marginBottom: 1, children: [options.map((option, i) => (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { children: [_jsxs(Text, { color: colors.text, bold: true, children: [i + 1, ".", ' '] }), _jsx(Text, { color: colors.text, children: option.label })] }), _jsxs(Text, { color: colors.secondary, children: [" ", option.description] }), _jsxs(Text, { color: colors.secondary, children: [" ", option.path] })] }, option.path))), _jsxs(Text, { color: colors.secondary, children: ["Usage: /setup-config ", '<number>', " to open in", ' ', _jsx(Text, { color: colors.text, children: editor })] })] }));
}
export const setupConfigCommand = {
    name: 'setup-config',
    description: 'Open a configuration file in your editor',
    handler: async (args) => {
        const options = getConfigOptions();
        if (args.length === 0) {
            return React.createElement(SetupConfigResult, { options });
        }
        const selection = Number.parseInt(args[0] ?? '', 10);
        if (Number.isNaN(selection) ||
            selection < 1 ||
            selection > options.length) {
            return React.createElement(SetupConfigResult, {
                options,
                error: `Invalid selection "${args[0]}". Choose a number between 1 and ${options.length}.`,
            });
        }
        try {
            const selected = options[selection - 1];
            if (!selected) {
                return React.createElement(SetupConfigResult, {
                    options,
                    error: `Invalid selection "${args[0]}".`,
                });
            }
            openInEditor(selected.path);
            return React.createElement(SetupConfigResult, {
                options,
                selectedIndex: selection - 1,
            });
        }
        catch (error) {
            return React.createElement(SetupConfigResult, {
                options,
                error: `Failed to open editor: ${formatError(error)}`,
            });
        }
    },
};
//# sourceMappingURL=setup-config.js.map