"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCommand = void 0;
var Option_1 = require("./Option");
var BaseCommand = /** @class */ (function () {
    function BaseCommand() {
        var _this = this;
        this.usageExamples = function () { return " $ plexus " + _this.name() + " " + _this.optionsExampleArgs().join(' '); };
        this.generalDescription = function () { return ''; };
        this.options = function () { return []; };
        this.optionArgs = function (optValues, separator, nameConverter) {
            if (nameConverter === void 0) { nameConverter = function (k) { return k; }; }
            return _this.options().reduce(function (seed, option) {
                var value = optValues[option.longName];
                if (!value) {
                    return seed;
                }
                var name = "--" + nameConverter(option.longName);
                if (option.isFlag) {
                    return seed.concat("" + name);
                }
                var optionArgs = separator ? ["" + name + separator + value] : [name, value];
                return seed.concat(optionArgs);
            }, []);
        };
        this.optionsExampleArgs = function () {
            return _this.options()
                .filter(function (o) { return o.exampleValue || o.isFlag; })
                .reduce(function (seed, option) {
                if (option.isFlag) {
                    return seed.concat("-" + option.shortName);
                }
                return seed.concat(["-" + option.shortName, option.exampleValue]);
            }, []);
        };
    }
    BaseCommand.prototype.isVerbose = function (opts) {
        return !!opts && !!opts.verbose && opts.verbose !== 'false';
    };
    BaseCommand.prototype.log = function (msg) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        console.log("[" + this.name() + "] " + msg, args);
    };
    BaseCommand.prototype.register = function (builder) {
        var _this = this;
        var commandBuilder = builder.command(this.name()).description(this.generalDescription());
        this.options().forEach(function (o) { return commandBuilder.option((0, Option_1.getFlags)(o), o.description, o.defaultValue); });
        commandBuilder = commandBuilder.action(function (opts) {
            // need to do it manually :(
            // https://github.com/tj/commander.js/issues/44
            _this.log('Validating input args');
            var errors = _this.validateRequiredOpts(_this.options(), opts);
            if (errors.length > 0) {
                _this.fail(errors.join('\n'));
            }
            _this.log('Starting execution');
            if (opts.verbose === 'true') {
                _this.log("Passed options: " + _this.optionValuesToString(opts));
            }
            _this.action(opts)
                .then(function () { return _this.log('Completed successfully'); })
                .catch(function (e) { return _this.fail(e); });
        });
        var examples = this.usageExamples();
        if (examples) {
            commandBuilder.on('--help', function () {
                console.log('');
                console.log('  Examples:');
                console.log('');
                console.log(examples);
                console.log('');
            });
        }
    };
    BaseCommand.prototype.fail = function (error) {
        this.log('Finished with error', error);
        process.exit(1);
    };
    BaseCommand.prototype.optionValuesToString = function (opts) {
        return this.options().reduce(function (seed, option) { return seed + " " + option.longName + "=" + opts[option.longName]; }, '');
    };
    BaseCommand.prototype.validateRequiredOpts = function (options, commanderOpts) {
        if (options) {
            return options
                .filter(function (o) { return !!o.isRequired && !commanderOpts[o.longName]; })
                .map(function (o) { return "'" + (0, Option_1.getFlags)(o) + "' option is required"; });
        }
        return [];
    };
    return BaseCommand;
}());
exports.BaseCommand = BaseCommand;
//# sourceMappingURL=BaseCommand.js.map