"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 path_1 = require("path");
const TemplateTypesList_1 = require("../../../modules/TemplateTypesList");
const WizError_1 = require("../../../modules/WizError");
const install_1 = require("../../install");
const generateVueTemplate_1 = require("../../../modules/generateVueTemplate");
const cloneTemplate_1 = require("../../cloneTemplate");
const checkTemplateVersion_1 = require("../../checkTemplateVersion");
const getTemplateVersion_1 = require("../../../utils/getTemplateVersion");
const system_settings_1 = require("@ewizardjs/system-settings");
function initAction(content, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const templateVersion = typeof options.version === 'function' ? '' : options.version;
        checkTemplateAvailability(content);
        if (!options.template) {
            yield (0, checkTemplateVersion_1.default)(content, templateVersion);
        }
        const { baseDirectory, name, template: templateLink, currentDir, registry, token } = options;
        const template = (0, TemplateTypesList_1.getTemplateData)(content, { url: templateLink }, templateVersion);
        const dest = (!currentDir && name) ? (0, path_1.join)(baseDirectory, name) : baseDirectory;
        const templatePath = yield generateTemplate(template, dest, options);
        const rootPath = (0, path_1.join)(dest, template.root);
        const installOptions = {
            cwd: rootPath,
            registry,
            token,
        };
        yield (0, install_1.action)()([], installOptions);
        yield setTemplateSettings(rootPath, templatePath, template);
    });
}
exports.default = initAction;
function generateTemplate(template, dest, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const templatePath = yield (0, cloneTemplate_1.default)({ template }, options);
        yield (0, generateVueTemplate_1.default)(templatePath, dest, options, template);
        return templatePath;
    });
}
function setTemplateSettings(rootPath, templatePath, { url }) {
    return __awaiter(this, void 0, void 0, function* () {
        const version = yield (0, getTemplateVersion_1.getTemplateVersion)(templatePath);
        return (0, system_settings_1.setByPath)(rootPath, 'template', { url, version });
    });
}
function checkTemplateAvailability(content) {
    const types = (0, TemplateTypesList_1.getAvailableTypes)();
    if (!types.includes(content.type)) {
        throw new WizError_1.WizError('TEMPLATE_NAME_NOT_PROVIDED');
    }
    if ((0, TemplateTypesList_1.isSubtypesEnable)(content.type) && !content.subtype) {
        throw new WizError_1.WizError('SUBTYPE_NAME_NOT_PROVIDED');
    }
}
//# sourceMappingURL=action.js.map