import { config, getConfig, setConfig, resetConfig, isValidConfigKey } from '../services/config.js';
import { success, info } from '../utils/logger.js';
import { outputSuccess, outputError, isPrettyMode } from '../utils/output.js';
import { addEnhancedHelp } from '../utils/helpText.js';
export function registerConfigCommand(program) {
    const configCmd = program
        .command('config')
        .description('配置管理');
    // gen config set
    const setCmd = configCmd
        .command('set')
        .description('设置配置项')
        .argument('<key>', '配置键')
        .argument('<value>', '配置值')
        .action((key, value, options) => {
        const pretty = isPrettyMode(options);
        try {
            if (!isValidConfigKey(key)) {
                outputError('INVALID_CONFIG_KEY', `无效的配置键: ${key}。可用键: outputDir`, options);
                return;
            }
            setConfig(key, value);
            if (pretty) {
                success(`已设置 ${key} = ${value}`);
            }
            else {
                outputSuccess({ message: '配置已更新', key, value });
            }
        }
        catch (err) {
            outputError('CONFIG_SET_FAILED', `设置失败: ${err}`, options);
        }
    });
    addEnhancedHelp(setCmd, {
        examples: [
            '# 设置输出目录',
            '$ gen config set outputDir ./my-outputs',
        ],
        outputJson: `{
  "success": true,
  "data": { "message": "配置已更新", "key": "outputDir", "value": "./my-outputs" }
}`,
        relatedCommands: [
            { command: 'config list', description: '查看当前配置' },
            { command: 'config reset --confirm', description: '重置为默认配置' },
        ],
        notes: ['可用配置项: outputDir'],
    });
    // gen config list
    const listCmd = configCmd
        .command('list')
        .description('查看当前配置')
        .action((options) => {
        const pretty = isPrettyMode(options);
        try {
            const cfg = getConfig();
            if (pretty) {
                info(`Output Dir: ${cfg.outputDir}`);
                info(`配置文件: ${config.path}`);
            }
            else {
                outputSuccess({ config: cfg, config_path: config.path });
            }
        }
        catch (err) {
            outputError('CONFIG_LIST_FAILED', `获取配置失败: ${err}`, options);
        }
    });
    addEnhancedHelp(listCmd, {
        examples: ['$ gen config list'],
        outputJson: `{
  "success": true,
  "data": { "config": { "outputDir": "./gen-output" }, "config_path": "..." }
}`,
        relatedCommands: [
            { command: 'config set <key> <value>', description: '设置配置项' },
        ],
        notes: ['显示所有配置项的当前值和配置文件路径'],
    });
    // gen config reset
    configCmd
        .command('reset')
        .description('重置为默认配置（需要 --confirm）')
        .option('--confirm', '确认重置')
        .action((options) => {
        const pretty = isPrettyMode(options);
        try {
            if (!options.confirm) {
                outputError('CONFIRM_REQUIRED', '请使用 --confirm 确认重置操作', options);
                return;
            }
            resetConfig();
            if (pretty) {
                success('配置已重置为默认值');
            }
            else {
                outputSuccess({ message: '配置已重置为默认值' });
            }
        }
        catch (err) {
            outputError('CONFIG_RESET_FAILED', `重置失败: ${err}`, options);
        }
    });
}
//# sourceMappingURL=config.js.map