"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 };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var commander_1 = require("@serverless-devs/commander");
var core_1 = __importDefault(require("../utils/core"));
var lodash_1 = require("lodash");
var constant_1 = require("../constant");
var getGlobalArgs = core_1.default.getGlobalArgs, execCommand = core_1.default.execCommand, getTemplatePath = core_1.default.getTemplatePath, getYamlContent = core_1.default.getYamlContent;
var SpecialCommad = /** @class */ (function () {
    function SpecialCommad(command) {
        this.command = command;
    }
    SpecialCommad.prototype.init = function () {
        return __awaiter(this, void 0, void 0, function () {
            var argv, rawData, template, access, skipActions, debug, sub, _a, serverName, method;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if ((0, lodash_1.includes)(constant_1.COMMAND_LIST, process.argv[2]))
                            return [2 /*return*/];
                        argv = getGlobalArgs(process.argv.slice(2));
                        rawData = argv._, template = argv.template, access = argv.access, skipActions = argv["skip-actions"], debug = argv.debug;
                        if ((0, lodash_1.isEmpty)(rawData))
                            return [2 /*return*/];
                        sub = new commander_1.Command(rawData[0]);
                        sub.allowUnknownOption();
                        this.command.addCommand(sub);
                        return [4 /*yield*/, this.getParams(argv)];
                    case 1:
                        _a = _b.sent(), serverName = _a.serverName, method = _a.method;
                        return [4 /*yield*/, execCommand({
                                syaml: template,
                                serverName: serverName,
                                method: method,
                                args: process.argv.slice(2),
                                globalArgs: {
                                    access: access,
                                    skipActions: skipActions,
                                    debug: debug,
                                },
                            })];
                    case 2: return [2 /*return*/, _b.sent()];
                }
            });
        });
    };
    SpecialCommad.prototype.getParams = function (argv) {
        return __awaiter(this, void 0, void 0, function () {
            var rawData, template, spath, yamlData, servicesName;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        rawData = argv._, template = argv.template;
                        return [4 /*yield*/, getTemplatePath(template)];
                    case 1:
                        spath = _a.sent();
                        if (spath) {
                            process.env['templateFile'] = spath;
                        }
                        if (rawData.length === 1) {
                            return [2 /*return*/, {
                                    method: rawData[0],
                                }];
                        }
                        if (!(rawData.length === 2)) return [3 /*break*/, 3];
                        return [4 /*yield*/, getYamlContent(spath)];
                    case 2:
                        yamlData = _a.sent();
                        servicesName = (0, lodash_1.keys)((0, lodash_1.get)(yamlData, 'services'));
                        return [2 /*return*/, (0, lodash_1.includes)(servicesName, rawData[0])
                                ? {
                                    serverName: rawData[0],
                                    method: rawData[1],
                                }
                                : {
                                    method: rawData[0],
                                }];
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    return SpecialCommad;
}());
exports.default = SpecialCommad;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3BlY2lhbC1jb21tYWQvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSx3REFBc0U7QUFDdEUsdURBQWlDO0FBQ2pDLGlDQUFzRDtBQUN0RCx3Q0FBMkM7QUFFbkMsSUFBQSxhQUFhLEdBQW1ELGNBQUksY0FBdkQsRUFBRSxXQUFXLEdBQXNDLGNBQUksWUFBMUMsRUFBRSxlQUFlLEdBQXFCLGNBQUksZ0JBQXpCLEVBQUUsY0FBYyxHQUFLLGNBQUksZUFBVCxDQUFVO0FBRTdFO0lBQ0UsdUJBQW9CLE9BQXdCO1FBQXhCLFlBQU8sR0FBUCxPQUFPLENBQWlCO0lBQUcsQ0FBQztJQUMxQyw0QkFBSSxHQUFWOzs7Ozs7d0JBQ0UsSUFBSSxJQUFBLGlCQUFRLEVBQUMsdUJBQVksRUFBRSxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDOzRCQUFFLHNCQUFPO3dCQUM5QyxJQUFJLEdBQUcsYUF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