"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_lib_1 = require("@hediet/cli-lib");
const printCmdHelp_1 = require("./help/printCmdHelp");
const printCliHelp_1 = require("./help/printCliHelp");
const showGui_1 = require("./showGui");
/**
 * Processes command line arguments and invokes the handler
 * with data obtained from `getData` of the selected command.
 * Also processes `--help`, `--version` and other global flags.
 */
function runDefaultCli(options) {
    const cmdArgs = options.args || process.argv.slice(2);
    const result = options.cli.parse(cmdArgs);
    function showHelp() {
        console.log();
        if (!result.parsedArgs["help"] && result.errors) {
            for (const e of result.errors.errors) {
                console.error(e.message);
            }
            console.log();
        }
        if (result.selectedCmd) {
            printCmdHelp_1.printCmdHelp({
                appName: options.info.appName,
            }, result.selectedCmd);
            if (result.selectedCmd.name === undefined &&
                options.cli.cmds.length > 1) {
                console.log();
                console.log("----------------------");
                console.log();
                printCliHelp_1.printCliHelp(options.cli, options.info, true);
            }
        }
        else {
            printCliHelp_1.printCliHelp(options.cli, options.info);
        }
        console.log();
        if (result.errors.hasErrors) {
            process.exit(1);
        }
    }
    function run(data) {
        options.dataHandler(data).catch(e => {
            // todo
            console.error(e);
            process.exit(1);
        });
    }
    let verbose = false;
    if (result.parsedArgs["cli::verbose"]) {
        verbose = true;
    }
    if (result.parsedArgs["help"]) {
        showHelp();
        return;
    }
    else if (result.parsedArgs["version"]) {
        console.log(`version: ${options.info.version}`);
        return;
    }
    else if (result.parsedArgs["cli::gui"]) {
        showGui_1.showGui(options.cli, run, result.selectedCmd, verbose);
        return;
    }
    else if (result.parsedArgs["cli::schema"]) {
        const schema = cli_lib_1.cliToSchema(options.cli);
        const json = cli_lib_1.sSchema.serialize(schema);
        console.log(JSON.stringify(json, undefined, 4));
        return;
    }
    else if (result.parsedArgs["cli::json-args"] !== undefined) {
        const jsonStr = result.parsedArgs["cli::json-args"];
        const json = JSON.parse(jsonStr);
        const cmd = options.cli
            .getSerializer()
            .deserializeTyped(json)
            .unwrap();
        const data = cmd.getData();
        run(data);
        return;
    }
    else if (result.errors.hasErrors) {
        showHelp();
        return;
    }
    if (!result.dataFactory) {
        throw new Error("Error");
    }
    const data = result.dataFactory();
    run(data);
}
exports.runDefaultCli = runDefaultCli;
//# sourceMappingURL=runDefaultCli.js.map