"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
// import { Command, Commands, arg, isError, format, HelpError, unknownCommand } from '@prisma/cli'
const version_1 = require("./version");
const util_1 = require("util");
const help_1 = require("./help");
const utils_1 = require("./helpers/utils");
const run_1 = require("./run");
/**
 * CLI command
 */
class CLI {
    constructor(cmds) {
        this.cmds = cmds;
    }
    static new(cmds) {
        return new CLI(cmds);
    }
    async parse(argv) {
        // parse the args according to the following spec
        const args = utils_1.arg(argv, {
            '--help': Boolean,
            '-h': '--help',
            '--version': Boolean,
            '-v': '--version',
        });
        if (util_1.isError(args)) {
            return this.help(args.message);
        }
        if (args['--version']) {
            return version_1.Version.new().parse(argv);
        }
        if (args['--help']) {
            return this.help();
        }
        if (args._.length === 0 || args._.length === 1) {
            return run_1.Run.new().parse(args._);
        }
        // check if we have that subcommand
        const cmd = this.cmds[args._[0]];
        if (cmd) {
            return cmd.parse(args._.slice(1));
        }
        // unknown command
        return help_1.unknownCommand(CLI.help, args._[0]);
    }
    // help function
    help(error) {
        if (error) {
            return new help_1.HelpError(`\n${chalk_1.default.bold.red(`!`)} ${error}\n${CLI.help}`);
        }
        return CLI.help;
    }
}
exports.CLI = CLI;
// static help template
CLI.help = util_1.format(`
    ${chalk_1.default.bold.green('Prisma Viewer   ಠ_ಠ')}

    ${chalk_1.default.bold('Usage')}
      ${chalk_1.default.dim(`$`)} pv [path]
      
    ${chalk_1.default.bold('Options')}
      -h, --help       Displays this help message
    ${chalk_1.default.bold('Examples')}
      ${chalk_1.default.dim(`$`)} pv 
      ${chalk_1.default.dim(`$`)} pv ./schema.prisma
  `);
