"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebugCommandRegistry = exports.DEBUG_COMMAND_DESCRIBE = exports.DEBUG_COMMAND_PATH = void 0;
const constants_1 = require("@mablhq-internal/runtime/core/constants");
const describe_1 = require("../commandUtil/describe");
const constants_2 = require("../constants");
const debugCommands_1 = require("./debugCommands");
const artifact_1 = require("./debug_cmds/artifact");
const session_1 = require("./debug_cmds/session");
const steps_1 = require("./debug_cmds/steps");
const debugFailWrapper_1 = require("./debug_cmds/util/debugFailWrapper");
exports.DEBUG_COMMAND_PATH = 'mabl agent debug';
exports.DEBUG_COMMAND_DESCRIBE = 'Debug mabl test failures';
class FlagCaptor {
    constructor() {
        this.capturedPositionals = [];
        this.capturedOptions = [];
    }
    positional(name, opts) {
        this.capturedPositionals.push({
            name,
            type: opts?.type,
            describe: opts?.describe,
            required: opts?.demandOption === true,
            choices: opts?.choices,
            default: opts?.default,
        });
        return this;
    }
    option(name, opts) {
        this.capturedOptions.push({
            name,
            type: opts?.type,
            describe: opts?.describe,
            required: opts?.demandOption === true,
            choices: opts?.choices,
            default: opts?.default,
            alias: opts?.alias,
        });
        return this;
    }
    options(input, opts) {
        if (typeof input === 'string') {
            return this.option(input, opts);
        }
        for (const [name, def] of Object.entries(input)) {
            this.option(name, def);
        }
        return this;
    }
    conflicts() {
        return this;
    }
}
function walkRegistry(cmds, parentPath) {
    return cmds.map((cmd) => {
        const fullCommand = `${parentPath} ${cmd.command}`;
        if (cmd.kind === debugCommands_1.DebugCommandKind.Group) {
            const groupPath = `${parentPath} ${primaryName(cmd.command)}`;
            return {
                command: fullCommand,
                describe: cmd.describe,
                subcommands: walkRegistry(cmd.subcommands, groupPath),
            };
        }
        const captor = new FlagCaptor();
        try {
            void cmd.builder(captor);
        }
        catch {
        }
        const node = {
            command: fullCommand,
            describe: cmd.describe,
        };
        if (captor.capturedPositionals.length > 0) {
            node.positionals = captor.capturedPositionals;
        }
        if (captor.capturedOptions.length > 0) {
            node.options = captor.capturedOptions;
        }
        return node;
    });
}
function buildCommandListLeaf(rootPath, rootDescribe, getSubtree) {
    return (0, debugCommands_1.defineLeaf)({
        command: 'command-list',
        describe: 'Print every subcommand under this group as a structured tree, including positionals and flags. Run `<subcommand> --help` for the formatted yargs help text.',
        builder: (yargs) => {
            yargs.options((0, describe_1.getDescribeBuilderOptions)());
        },
        handler: (0, debugFailWrapper_1.debugFailWrapper)((args) => {
            const tree = {
                command: rootPath,
                describe: rootDescribe,
                subcommands: walkRegistry(getSubtree(), rootPath),
            };
            (0, describe_1.outputEntity)(tree, args[constants_2.CommandArgOutput] ?? constants_1.OutputFormats.Json);
        }),
    });
}
class DebugCommandRegistry {
    static register(yargs) {
        for (const cmd of DebugCommandRegistry.commands) {
            registerCommand(yargs, cmd);
        }
        registerCommand(yargs, buildCommandListLeaf(exports.DEBUG_COMMAND_PATH, exports.DEBUG_COMMAND_DESCRIBE, () => DebugCommandRegistry.commands));
        return yargs;
    }
}
exports.DebugCommandRegistry = DebugCommandRegistry;
DebugCommandRegistry.commands = [
    artifact_1.artifactCommand,
    session_1.sessionCommand,
    steps_1.stepsCommand,
];
function registerCommand(yargs, cmd) {
    if (cmd.kind === debugCommands_1.DebugCommandKind.Group) {
        yargs.command({
            command: cmd.command,
            describe: cmd.describe,
            builder: (sub) => {
                for (const child of cmd.subcommands) {
                    registerCommand(sub, child);
                }
                registerCommand(sub, buildCommandListLeaf(`${exports.DEBUG_COMMAND_PATH} ${primaryName(cmd.command)}`, cmd.describe, () => cmd.subcommands));
                return sub.demandCommand();
            },
            handler: () => undefined,
        });
        return;
    }
    yargs.command({
        command: cmd.command,
        describe: cmd.describe,
        builder: cmd.builder,
        handler: cmd.handler,
    });
}
function primaryName(command) {
    return command.split(/\s+/)[0] ?? '';
}
