"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 });
exports.action = void 0;
const upath_1 = require("@ewizardjs/upath");
const WorkersListr_1 = require("../../modules/WorkersListr");
const getRepoInfo_1 = require("../../utils/getRepoInfo");
const utils_1 = require("./utils");
const LAYOUT_WORKER = (0, upath_1.join)(__dirname, 'layout.worker.js');
const EWIZARD_TEMPLATE_WORKER = (0, upath_1.join)(__dirname, 'ewizard-template.worker.js');
function action(baseDir, options) {
    return __awaiter(this, void 0, void 0, function* () {
        return runCreateCommand(baseDir, options);
    });
}
exports.action = action;
function runCreateCommand(baseDir, options) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        const layoutsDir = yield (0, utils_1.getLayoutsDir)(baseDir);
        const layoutsList = options.fromAll
            ? yield (0, utils_1.getAllLayouts)((0, upath_1.join)(baseDir, layoutsDir))
            : (0, utils_1.getUniqeLayouts)((_a = options.from) !== null && _a !== void 0 ? _a : []);
        switch (options.command) {
            case 'layout':
                return createLayoutContent(baseDir, layoutsDir, layoutsList, options);
            case 'ewizard-template':
                return createEwizardTemplateContent(baseDir, layoutsDir, layoutsList, options);
        }
    });
}
function createLayoutContent(baseDir, layoutsDir, layoutsList, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const listr = new WorkersListr_1.WorkersListr(LAYOUT_WORKER);
        const layoutTasks = layoutsList.map((layoutName) => (0, utils_1.getCreateTask)(baseDir, layoutsDir, layoutName, options));
        return listr.add(layoutTasks).run();
    });
}
function createEwizardTemplateContent(baseDir, layoutsDir, layoutsList, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const listr = new WorkersListr_1.WorkersListr(EWIZARD_TEMPLATE_WORKER, undefined, {
            concurrent: false,
            exitOnError: true,
        });
        const masterRepoInfo = yield (0, getRepoInfo_1.getRepoInfo)(baseDir);
        const createOptions = Object.assign(Object.assign({}, options), { masterRepoInfo });
        const installTaskOptions = {
            ctx: {
                dest: (0, upath_1.join)(baseDir, 'build', options.command),
                options: createOptions,
                baseDir,
            },
        };
        const ewizardTemplateTasksOptions = {
            concurrent: true,
            exitOnError: false,
        };
        const installTask = [(0, utils_1.getInstallTask)()];
        const ewizardTemplateTasks = options.fromMasterTemplate
            ? [(0, utils_1.getCreateTask)(baseDir, layoutsDir, masterRepoInfo.repoName, createOptions)]
            : layoutsList.map((layoutName) => (0, utils_1.getCreateTask)(baseDir, layoutsDir, layoutName, createOptions));
        return listr
            .add(installTask, installTaskOptions)
            .add(ewizardTemplateTasks, ewizardTemplateTasksOptions)
            .run();
    });
}
//# sourceMappingURL=action.js.map