"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 utils_1 = require("../utils");
var lodash_1 = require("lodash");
var core_1 = __importDefault(require("../utils/core"));
var path_1 = __importDefault(require("path"));
var makeUnderLine = core_1.default.makeUnderLine, publishHelp = core_1.default.publishHelp, getGlobalArgs = core_1.default.getGlobalArgs, getTemplatePath = core_1.default.getTemplatePath, getYamlContent = core_1.default.getYamlContent, loadComponent = core_1.default.loadComponent;
var descption = {
    Options: [
        { '--debug': 'Open debug model.' },
        { '--skip-actions': 'Skip the extends section.' },
        { '-t, --template <path>': 'Specify the template file.' },
        { '-a, --access <aliasName>': 'Specify the access alias name.' },
        { '-v, --version': 'Output the version number.' },
        { '-h, --help': 'Display help for command.' },
    ],
    Commands: [
        { config: '👤  Configure venders account.' },
        { init: '💞  Initializing a serverless project.' },
        { cli: '🐚  Command line operation without yaml mode.' },
        { verify: '🔎  Verify the application.' },
        { set: '🔧  Settings for the tool.' },
        { clean: '💥  Clean up the environment.' },
        { component: '🔌  Installed component information.' },
    ],
    Examples: [{ init: 'Perform [s init] fast experience Serverless Devs.' }],
};
function help(program) {
    return __awaiter(this, void 0, void 0, function () {
        var helperLength, _a, rawData, template, help, customeDescription, spath, yamlData, serviceList, _i, serviceList_1, service, component, instance, publishPath, publishContent, commands, key, error_1, output;
        var _b, _c;
        return __generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    helperLength = publishHelp.maxLen(descption.Options);
                    _a = getGlobalArgs(process.argv.slice(2)), rawData = _a._, template = _a.template, help = _a.help;
                    customeDescription = [];
                    if (!(rawData.length === 0 && help)) return [3 /*break*/, 9];
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 8, , 9]);
                    return [4 /*yield*/, getTemplatePath(template)];
                case 2:
                    spath = _d.sent();
                    if (!spath) return [3 /*break*/, 7];
                    return [4 /*yield*/, getYamlContent(spath)];
                case 3:
                    yamlData = _d.sent();
                    serviceList = (0, lodash_1.keys)((0, lodash_1.get)(yamlData, 'services'));
                    if (!(serviceList.length > 1)) return [3 /*break*/, 4];
                    for (_i = 0, serviceList_1 = serviceList; _i < serviceList_1.length; _i++) {
                        service = serviceList_1[_i];
                        customeDescription.push((_b = {},
                            _b["".concat(service, " [options]")] = "Please use [s ".concat(service, " -h]  obtain the documentation."),
                            _b));
                    }
                    return [3 /*break*/, 7];
                case 4:
                    component = (0, lodash_1.get)(yamlData, ['services', serviceList[0], 'component']);
                    return [4 /*yield*/, loadComponent(component)];
                case 5:
                    instance = _d.sent();
                    publishPath = path_1.default.join(instance.__path, 'publish.yaml');
                    return [4 /*yield*/, getYamlContent(publishPath)];
                case 6:
                    publishContent = _d.sent();
                    commands = publishContent.Commands;
                    if (commands) {
                        for (key in commands) {
                            customeDescription.push((_c = {}, _c[key] = commands[key], _c));
                        }
                    }
                    _d.label = 7;
                case 7: return [3 /*break*/, 9];
                case 8:
                    error_1 = _d.sent();
                    return [3 /*break*/, 9];
                case 9:
                    output = [
                        "".concat((0, utils_1.emoji)('🚀'), " Welcome to the Serverless Devs.\n"),
                        publishHelp.helpInfo(descption.Options, 'Options', helperLength),
                        publishHelp.helpInfo(descption.Commands, 'Commands', helperLength),
                        publishHelp.helpInfo(customeDescription, 'Custom Commands', helperLength),
                        publishHelp.helpInfo(descption.Examples, 'Examples', helperLength),
                        "".concat((0, utils_1.emoji)('🧭'), " ").concat(makeUnderLine('More information: https://github.com/Serverless-Devs/Serverless-Devs'), " ") + '\n',
                    ].join('\n');
                    program.on('--help', function () {
                        console.log(output);
                    });
                    return [2 /*return*/, output];
            }
        });
    });
}
exports.default = help;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaGVscC9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7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