// SPDX-License-Identifier: Apache-2.0
import { SoloError } from '../errors/solo-error.js';
export class Subcommand {
    name;
    description;
    commandHandlerClass;
    commandHandler;
    builder;
    constructor(name, description, commandHandlerClass, commandHandler, builder) {
        this.name = name;
        this.description = description;
        this.commandHandlerClass = commandHandlerClass;
        this.commandHandler = commandHandler;
        this.builder = builder;
    }
}
export class CommandGroup {
    name;
    description;
    subcommands = [];
    constructor(name, description) {
        this.name = name;
        this.description = description;
    }
    addSubcommand(subcommand) {
        this.subcommands.push(subcommand);
        return this;
    }
}
export class CommandBuilder {
    name;
    description;
    logger;
    commandGroups = [];
    constructor(name, description, logger) {
        this.name = name;
        this.description = description;
        this.logger = logger;
    }
    addCommandGroup(commandGroup) {
        this.commandGroups.push(commandGroup);
        return this;
    }
    build() {
        const commandGroups = this.commandGroups;
        const logger = this.logger;
        const commandName = this.name;
        const commandDescription = this.description;
        const demandCommand = `select a ${commandName} command`;
        return {
            command: commandName,
            desc: commandDescription,
            builder: (yargs) => {
                for (const commandGroup of commandGroups) {
                    yargs.command({
                        command: commandGroup.name,
                        desc: commandGroup.description,
                        builder: (yargs) => {
                            yargs.help(); // Enable help for command group level
                            for (const subcommand of commandGroup.subcommands) {
                                const handlerDefinition = {
                                    command: subcommand.name,
                                    desc: subcommand.description,
                                    handler: async (argv) => {
                                        const commandPath = `${commandName} ${commandGroup.name} ${subcommand.name}`;
                                        logger.info(`==== Running '${commandPath}' ===`);
                                        const handlerCallback = subcommand.commandHandler.bind(subcommand.commandHandlerClass);
                                        const response = await handlerCallback(argv);
                                        logger.info(`==== Finished running '${commandPath}'====`);
                                        if (!response) {
                                            throw new SoloError(`Error running ${commandPath}, expected return value to be true`);
                                        }
                                    },
                                };
                                if (subcommand.builder) {
                                    handlerDefinition.builder = subcommand.builder;
                                }
                                yargs.command(handlerDefinition);
                            }
                            yargs.demandCommand(1, `Select a ${commandName} ${commandGroup.name} command`);
                            return yargs.help();
                        },
                    });
                }
                yargs.demandCommand(1, demandCommand);
                return yargs.help();
            },
        };
    }
}
//# sourceMappingURL=command-builder.js.map