"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander = require("commander");
const inquirer_1 = require("inquirer");
const uuid_1 = require("uuid");
const init_1 = require("../Api/scaffolding/init");
const ewizardService_types_1 = require("../interfaces/ewizardService.types");
const I18n_1 = require("../modules/I18n");
const TemplateTypesList_1 = require("../modules/TemplateTypesList");
const codeArtifactUtils_1 = require("../utils/codeArtifactUtils");
const ensureAuth_1 = require("./utils/ensureAuth");
const getInquirerListType_1 = require("./utils/getInquirerListType");
const logger_1 = require("./utils/logger");
commander
    .name('wiz init')
    .option('-d, --default', 'set default prompts')
    .option('-t, --template <link>', 'set template link')
    .option('-v, --template-version <version>', 'set template version')
    .parse(process.argv);
let [type, projectName] = commander.args;
const opts = commander.opts();
const [, version] = (opts.template || '').split('#');
const options = Object.assign(Object.assign({}, opts), { version: opts.templateVersion || version, baseDirectory: process.cwd(), name: projectName, id: (0, uuid_1.v4)() });
initTemplate({ type }, options)
    .catch(logger_1.handleError)
    .finally(logger_1.closeLogger);
function initTemplate(content, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const ewizard = yield (0, ensureAuth_1.default)();
        const cwd = process.cwd();
        const npmRegistryParams = yield (0, codeArtifactUtils_1.getNpmRegistryParams)(options, cwd, ewizardService_types_1.ActionType.Install);
        const npmRegistryConfig = yield ewizard.getNpmRegistry(npmRegistryParams);
        const initOptions = Object.assign(Object.assign(Object.assign({}, options), npmRegistryConfig), { cwd });
        return (0, init_1.initAction)(content, initOptions)
            .catch(processError(options));
    });
}
function processError(options) {
    return (err) => __awaiter(this, void 0, void 0, function* () {
        let subtype = '';
        if (err.code === 'TEMPLATE_NAME_NOT_PROVIDED') {
            const message = I18n_1.default.info('CHOOSE_TYPE');
            const choices = (0, TemplateTypesList_1.getAvailableTypes)();
            type = yield createSelectList(message, choices);
            return initTemplate({ type, subtype }, options);
        }
        if (err.code === 'SUBTYPE_NAME_NOT_PROVIDED') {
            const message = I18n_1.default.info('CHOOSE_SUBTYPE');
            const choices = (0, TemplateTypesList_1.getAvailableSubtypes)(type);
            subtype = yield createSelectList(message, choices);
            return initTemplate({ type, subtype }, options);
        }
        throw err;
    });
}
function createSelectList(message, choices) {
    return (0, inquirer_1.prompt)([
        {
            message,
            choices,
            pageSize: choices.length + 1,
            type: (0, getInquirerListType_1.default)(),
            name: 'value',
        },
    ]).then(({ value }) => value);
}
//# sourceMappingURL=wiz-init.js.map