"use strict";
const fs = require("fs");
const path = require("path");
require("colors");
const errors = require("./errors");
const options = require("./options");
class CommandExecutor {
    execute() {
        var commandName = this.getCommandName();
        var commandArguments = this.getCommandArguments();
        return this.executeCore(commandName, commandArguments);
    }
    executeCore(commandName, commandArguments) {
        try {
            let filePath = path.join(__dirname, "commands", commandName + ".js");
            if (fs.existsSync(filePath)) {
                var command = new (require(filePath).Command)();
                if (!command) {
                    errors.fail("Unable to resolve commandName %s", commandName);
                }
                command.execute(commandArguments);
            }
        }
        catch (e) {
            if (options.debug) {
                throw e;
            }
            else {
                console.log("\x1B[31;1m" + e.message + "\x1B[0m");
            }
        }
    }
    getCommandArguments() {
        var remaining = options._;
        return remaining.length > 1 ? remaining.slice(1) : [];
    }
    getCommandName() {
        var remaining = options._;
        return remaining.length > 0 ? remaining[0].toLowerCase() : "help";
    }
}
exports.CommandExecutor = CommandExecutor;
