"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var commander_1 = require("commander");
var core_1 = require("@serverless-devs/core");
var utils_1 = require("../utils");
var CliSubCommandManager = /** @class */ (function () {
    function CliSubCommandManager(cliCommand, subCommandName) {
        this.cliCommand = cliCommand;
        this.subCommandName = subCommandName;
    }
    CliSubCommandManager.prototype.init = function () {
        return __awaiter(this, void 0, void 0, function () {
            var execCommand, customerCommandDescription, subCommandInstance, listApi;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        execCommand = new commander_1.Command(this.subCommandName);
                        customerCommandDescription = utils_1.i18n.__("Subcommand execution analysis");
                        execCommand.usage("[method] [options]");
                        return [4 /*yield*/, core_1.loadComponent(this.subCommandName)];
                    case 1:
                        subCommandInstance = _a.sent();
                        if (subCommandInstance) {
                            listApi = subCommandInstance.__listApi();
                            execCommand.description(customerCommandDescription).addHelpCommand(true);
                            listApi.forEach(function (item) {
                                var name = item.name, desc = item.desc, params = item.params;
                                var apiCommand = new commander_1.Command(name);
                                apiCommand.description(desc);
                                params.forEach(function (param) {
                                    apiCommand.option("--" + param.paramName + " [" + param.paramName + "]", (param.type ? param.type + " \u7C7B\u578B" : '') + ":" + param.paramDesc);
                                });
                                apiCommand.addHelpCommand(false);
                                apiCommand.action(function () { return __awaiter(_this, void 0, void 0, function () {
                                    var paramsList, result;
                                    return __generator(this, function (_a) {
                                        switch (_a.label) {
                                            case 0:
                                                paramsList = params.map(function (_item) { return apiCommand[_item.paramName]; }).filter(function (item) { return item; });
                                                if (paramsList.length === 0) {
                                                    apiCommand.help();
                                                }
                                                return [4 /*yield*/, subCommandInstance[name].apply(subCommandInstance, paramsList)];
                                            case 1:
                                                result = _a.sent();
                                                utils_1.logger.info(result);
                                                return [2 /*return*/];
                                        }
                                    });
                                }); });
                                execCommand.addCommand(apiCommand);
                            });
                            this.cliCommand.addCommand(execCommand);
                        }
                        return [2 /*return*/, this.cliCommand];
                }
            });
        });
    };
    return CliSubCommand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