import { registerAuthCommands } from './auth.js';
import { BridgeCommand } from './bridge.js';
import { CancelCommand } from './cancel.js';
import { ChatCommand } from './chat.js';
import { ConfigCommand } from './config.js';
import { DeployCommand } from './deploy.js';
import { EditCommand } from './edit.js';
import { ExplainCommand } from './explain.js';
import { ForkCommand } from './fork.js';
import { GenerateCommand } from './generate.js';
import { HistoryCommand } from './history.js';
import { HubCommand } from './hub.js';
import { LegionCommand } from './legion.js';
import { MusicCommand } from './music.js';
import { PreviewCommand } from './preview.js';
import { ReplayCommand } from './replay.js';
import { RepoCommand } from './repo.js';
import { ReviewCommand } from './review.js';
import { WorkflowCommand } from './workflow.js';
import { V4Command } from './v4.js';
function isRegisterableCommand(value) {
    return Boolean(value && typeof value === 'object');
}
function invokeRegistrar(instance, program) {
    if (!isRegisterableCommand(instance)) {
        throw new Error('Command instance is not registerable');
    }
    const registrar = instance.register ?? instance.setup ?? instance.configure;
    if (typeof registrar !== 'function') {
        throw new Error('Command does not expose register(), setup(), or configure()');
    }
    registrar.call(instance, program);
}
function createClassRegistrar(CommandClass) {
    return (program) => {
        invokeRegistrar(new CommandClass(), program);
    };
}
function registerDoctorCommand(program) {
    program
        .command('doctor')
        .description('Run local Vigthoria CLI diagnostics')
        .action(() => {
        const checks = [
            ['Node.js', process.version],
            ['Platform', `${process.platform} ${process.arch}`],
            ['Working directory', process.cwd()],
        ];
        console.log('Vigthoria CLI diagnostics');
        for (const [label, value] of checks) {
            console.log(`- ${label}: ${value}`);
        }
    });
}
function registerVersionInfoCommand(program) {
    program
        .command('version-info')
        .description('Show runtime version information')
        .action(() => {
        console.log(JSON.stringify({
            node: process.version,
            platform: process.platform,
            arch: process.arch,
            argv0: process.argv0,
        }, null, 2));
    });
}
const commandRegistry = [
    {
        name: 'auth',
        handler: registerAuthCommands,
    },
    {
        name: 'chat',
        handler: createClassRegistrar(ChatCommand),
    },
    {
        name: 'edit',
        handler: createClassRegistrar(EditCommand),
    },
    {
        name: 'generate',
        handler: createClassRegistrar(GenerateCommand),
    },
    {
        name: 'explain',
        handler: createClassRegistrar(ExplainCommand),
    },
    {
        name: 'config',
        handler: createClassRegistrar(ConfigCommand),
    },
    {
        name: 'review',
        handler: createClassRegistrar(ReviewCommand),
    },
    {
        name: 'hub',
        handler: createClassRegistrar(HubCommand),
    },
    {
        name: 'music',
        aliases: ['audio'],
        handler: createClassRegistrar(MusicCommand),
    },
    {
        name: 'repo',
        handler: createClassRegistrar(RepoCommand),
    },
    {
        name: 'deploy',
        handler: createClassRegistrar(DeployCommand),
    },
    {
        name: 'bridge',
        handler: createClassRegistrar(BridgeCommand),
    },
    {
        name: 'v4',
        aliases: ['v4-agent'],
        handler: createClassRegistrar(V4Command),
    },
    {
        name: 'workflow',
        handler: createClassRegistrar(WorkflowCommand),
    },
    {
        name: 'preview',
        handler: createClassRegistrar(PreviewCommand),
    },
    {
        name: 'legion',
        handler: createClassRegistrar(LegionCommand),
    },
    {
        name: 'history',
        handler: createClassRegistrar(HistoryCommand),
    },
    {
        name: 'replay',
        handler: createClassRegistrar(ReplayCommand),
    },
    {
        name: 'fork',
        handler: createClassRegistrar(ForkCommand),
    },
    {
        name: 'cancel',
        handler: createClassRegistrar(CancelCommand),
    },
    {
        name: 'doctor',
        handler: registerDoctorCommand,
    },
    {
        name: 'version-info',
        aliases: ['versionInfo'],
        handler: registerVersionInfoCommand,
    },
];
function normalizeCommandName(name) {
    return name.trim().toLowerCase();
}
function commandNameMatches(entry, requestedName) {
    if (normalizeCommandName(entry.name) === requestedName) {
        return true;
    }
    return Boolean(entry.aliases?.some((alias) => normalizeCommandName(alias) === requestedName));
}
export function resolveCommand(name) {
    const requestedName = normalizeCommandName(name);
    if (!requestedName) {
        return null;
    }
    const entry = commandRegistry.find((candidate) => commandNameMatches(candidate, requestedName));
    return entry?.handler ?? null;
}
export function registerCommands(cli) {
    if (!cli || typeof cli.command !== 'function') {
        throw new Error('registerCommands requires a Commander-compatible CLI instance');
    }
    const program = cli;
    for (const entry of commandRegistry) {
        entry.handler(program);
    }
}
export const setupCommands = registerCommands;
export const configureCommands = registerCommands;
export default registerCommands;
