import inquirer from 'inquirer';
const AGENT_COMMANDS = new Set([
    'agent', 'chat', 'chat-resume', 'operator', 'legion', 'workflow',
    'history', 'replay', 'fork', 'cancel',
]);
function familyFor(commandPath) {
    const root = commandPath.split(' ')[0];
    if (root === 'repo')
        return 'Vigthoria Repo Management';
    if (AGENT_COMMANDS.has(root))
        return 'Agent Mode';
    return 'System & Configuration';
}
export function buildCommandCatalog(program) {
    const entries = [];
    const visit = (command, parents) => {
        const commandPath = [...parents, command.name()].join(' ');
        entries.push({
            path: commandPath,
            description: command.description() || '',
            family: familyFor(commandPath),
            command,
        });
        for (const child of command.commands)
            visit(child, [...parents, command.name()]);
    };
    for (const command of program.commands)
        visit(command, []);
    return entries.sort((left, right) => left.family.localeCompare(right.family) || left.path.localeCompare(right.path));
}
export function renderDynamicHelp(program) {
    const catalog = buildCommandCatalog(program);
    const lines = ['Commands:'];
    const families = [
        'Vigthoria Repo Management',
        'Agent Mode',
        'System & Configuration',
    ];
    for (const family of families) {
        lines.push('', `${family}:`);
        for (const entry of catalog.filter((candidate) => candidate.family === family)) {
            const aliases = entry.command.aliases();
            const displayPath = aliases.length
                ? `${entry.path} (${aliases.join(', ')})`
                : entry.path;
            lines.push(`  ${displayPath.padEnd(30)} ${entry.description}`);
        }
    }
    return lines.join('\n');
}
async function collectRequiredArguments(entry) {
    const values = [];
    for (const argument of entry.command.registeredArguments) {
        if (!argument.required)
            continue;
        const answer = await inquirer.prompt([{
                type: 'input',
                name: 'value',
                message: `${argument.name()}:`,
                validate: (value) => value.trim().length > 0 || `${argument.name()} is required`,
            }]);
        values.push(String(answer.value).trim());
    }
    return values;
}
export async function selectInteractiveCommand(program) {
    const catalog = buildCommandCatalog(program);
    const familyAnswer = await inquirer.prompt([{
            type: 'list',
            name: 'family',
            message: 'Select a command family:',
            choices: [
                'Vigthoria Repo Management',
                'Agent Mode',
                'System & Configuration',
                new inquirer.Separator(),
                { name: 'Exit', value: null },
            ],
        }]);
    if (!familyAnswer.family)
        return null;
    const entries = catalog.filter((entry) => entry.family === familyAnswer.family && entry.command.commands.length === 0);
    const commandAnswer = await inquirer.prompt([{
            type: 'list',
            name: 'path',
            message: `${familyAnswer.family}:`,
            pageSize: 20,
            choices: entries.map((entry) => ({
                name: `${entry.path.padEnd(24)} ${entry.description}`,
                value: entry.path,
            })),
        }]);
    const selected = entries.find((entry) => entry.path === commandAnswer.path);
    if (!selected)
        return null;
    return [...selected.path.split(' '), ...await collectRequiredArguments(selected)];
}
