"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 system_settings_1 = require("@ewizardjs/system-settings");
const commander = require("commander");
const path_1 = require("path");
const dev_1 = require("../Api/dev");
const monorepo_1 = require("../Api/monorepo");
const I18n_1 = require("../modules/I18n");
const Logger_1 = require("../modules/Logger");
const WizError_1 = require("../modules/WizError");
const validateLayoutPath_1 = require("../utils/validateLayoutPath");
const ensureAuth_1 = require("./utils/ensureAuth");
const getDevOptions_1 = require("./utils/getDevOptions");
const validateMonorepo_1 = require("./utils/validateMonorepo");
commander
    .name('wiz dev')
    .option('--layout <layout>', 'DEPRECATED: Run dev build with layout')
    .option('--watch', 'Run dev build with changes observer')
    .option('--live', 'Run dev build with live-server')
    .option('--fix', 'Run dev build with error prevention')
    .option('--debug', 'Run dev build in debug mode')
    .option('--no-template', 'Run dev build without templates')
    .option('--from <workspace...>', 'Run a development build for specified workspaces or layout. NOTE: Available for emails only')
    .option('--from-all', 'Run a development build for every workspace in your email project. NOTE: Not available for master templates')
    .option('--validate', 'Run a development build with validation')
    .parse(process.argv);
dev(process.cwd(), commander.opts()).catch((err) => Logger_1.default.error(err.message));
function dev(baseDir, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const isMonorepo = yield validateMonorepo_1.isMonorepo(baseDir);
        if (options.layout) {
            Logger_1.default.info(I18n_1.default.info('LAYOUT_OPTION_DEPRECATED', 'wiz dev --from <layout>'));
            options.from = [options.layout];
        }
        validateArgs(options, isMonorepo);
        yield ensureAuth_1.default();
        if (isMonorepo) {
            yield validateMonorepo_1.validateMonorepo(baseDir);
            return monorepo_1.action(baseDir, Object.assign(Object.assign({}, options), { command: 'dev' }));
        }
        else if (options.from) {
            const layoutPath = yield getLayoutPath(baseDir, options.from[0]);
            yield validateLayoutPath_1.validateLayoutPath(layoutPath);
        }
        const devAction = yield dev_1.createAction(baseDir);
        const devOptions = getDevOptions_1.getDevOptions(options);
        return devAction(baseDir, devOptions);
    });
}
function validateArgs(options, isMonorepo) {
    if (!isMonorepo && validateMonorepo_1.passedMultipleWorkspaces(options)) {
        throw new WizError_1.WizError('ONLY_ONE_LAYOUT_ALLOWED');
    }
    else if (isMonorepo && !validateMonorepo_1.areWorkspacesPassed(options)) {
        throw new WizError_1.WizError('OPTION_NOT_SPECIFIED', '--from <workspace...>');
    }
    if (isMonorepo && passedNonMonorepoOptions(options)) {
        Logger_1.default.info(I18n_1.default.info('PASSED_NON_MONOREPO_OPTIONS'));
    }
}
function passedNonMonorepoOptions(options) {
    return options.debug || options.live || options.watch || options.fix || options.validate;
}
function getLayoutPath(baseDir, layoutName) {
    return __awaiter(this, void 0, void 0, function* () {
        const layoutsDir = yield system_settings_1.getByPath(baseDir, 'path.layouts');
        return path_1.join(layoutsDir, layoutName);
    });
}
//# sourceMappingURL=wiz-dev.js.map