import { isSea } from "node:sea";
import { Command, CommanderError } from "commander";
import { createConfigCommand } from "./commands/config/index.js";
import { createDemoCommand } from "./commands/demo/index.js";
import { createDeploymentCommands } from "./commands/deployment/index.js";
import { createDoctorCommand } from "./commands/doctor/index.js";
import { createInitCommand } from "./commands/init/index.js";
import { createInstanceCommand } from "./commands/instance/index.js";
import { createStatusCommand } from "./commands/status/index.js";
import { createVersionCommand, versionText } from "./commands/version/index.js";
export async function runCli(args, context = defaultContext()) {
    let actionExitCode = 0;
    const setExitCode = (exitCode) => {
        actionExitCode = exitCode;
    };
    const program = createProgram(context, setExitCode);
    try {
        await program.parseAsync(args, { from: "user" });
        return actionExitCode;
    }
    catch (error) {
        if (error instanceof CommanderError) {
            return error.exitCode;
        }
        context.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
        return 1;
    }
}
function createProgram(context, setExitCode) {
    const program = new Command()
        .name("eudiplo")
        .description("Deployment-aware command line tools for EUDIPLO")
        .version(versionText(), "-v, --version", "Print the installed CLI version")
        .helpCommand(true)
        .addHelpText("afterAll", "\nFor more information, see https://openwallet-foundation.github.io/eudiplo/docs/latest/getting-started/cli/");
    program.addCommand(createDemoCommand(context, setExitCode));
    program.addCommand(createInitCommand(context, setExitCode));
    for (const command of createDeploymentCommands(context, setExitCode)) {
        program.addCommand(command);
    }
    program.addCommand(createInstanceCommand(context, setExitCode));
    program.addCommand(createConfigCommand(context, setExitCode));
    program.addCommand(createDoctorCommand(context, setExitCode));
    program.addCommand(createStatusCommand(context, setExitCode));
    program.addCommand(createVersionCommand(context, setExitCode));
    program.action(function showRootHelp() {
        this.outputHelp();
    });
    configureCommandTree(program, context);
    return program;
}
function configureCommandTree(command, context) {
    command.exitOverride();
    command.configureOutput({
        writeOut: (value) => context.stdout.write(value),
        writeErr: (value) => context.stderr.write(value),
    });
    command.showHelpAfterError("(add --help for additional information)");
    for (const child of command.commands) {
        configureCommandTree(child, context);
    }
}
function defaultContext() {
    return {
        cwd: process.cwd(),
        env: process.env,
        installationMethod: isSea() ? "standalone" : "npm",
        interactive: process.stdin.isTTY === true && process.stdout.isTTY === true,
        stdout: process.stdout,
        stderr: process.stderr,
        fetch,
    };
}
//# sourceMappingURL=runtime.js.map