"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 content_checker_1 = require("@ewizardjs/content-checker");
const commander = require("commander");
const create_1 = require("../Api/create");
const monorepo_1 = require("../Api/monorepo");
const Logger_1 = require("../modules/Logger");
const WizError_1 = require("../modules/WizError");
const I18n_1 = require("./../modules/I18n");
const archiveContent_1 = require("./utils/archiveContent");
const ensureAuth_1 = require("./utils/ensureAuth");
const logger_1 = require("./utils/logger");
const validateMonorepo_1 = require("./utils/validateMonorepo");
commander
    .name('wiz archive')
    .storeOptionsAsProperties(false)
    .option('-l, --layout <layoutName...>', 'DEPRECATED: Run dev build and archive content in layout folder')
    .option('--name <archiveName>', 'Set archive name')
    .option('--target <targetClmName>', 'Set target CLM')
    .option('--theme <themeName>', 'Set theme name')
    .option('--scheme <schemeName>', 'Set scheme name')
    .option('--from <workspace...>', 'Create an archive for the specified workspace. NOTE: Available for emails only')
    .option('--from-all', 'Create an archive for every workspace in your project. NOTE: Available for emails only')
    .parse(process.argv);
archive(process.cwd(), commander.opts())
    .catch(logger_1.handleError)
    .finally(logger_1.closeLogger);
function archive(baseDir, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { layout } = options;
        const isMonorepo = yield validateMonorepo_1.isMonorepo(baseDir);
        validateArgs(options, isMonorepo);
        yield ensureAuth_1.default();
        if (layout) {
            Logger_1.default.info(I18n_1.default.info('LAYOUT_OPTION_DEPRECATED', 'wiz create layout --from <layout>'));
            return runCreateLayoutAction(baseDir, options);
        }
        if (isMonorepo) {
            yield validateMonorepo_1.validateMonorepo(baseDir);
            return monorepo_1.action(baseDir, Object.assign(Object.assign({}, options), { command: 'archive' }));
        }
        yield content_checker_1.validateEwizardContent(baseDir);
        yield content_checker_1.validateInstalledDependencies(baseDir);
        yield archiveContent_1.archiveContent(baseDir, options);
        Logger_1.default.info(I18n_1.default.info('ARCHIVE_CREATED'));
    });
}
function validateArgs(options, isMonorepo) {
    if (isMonorepo && !validateMonorepo_1.areWorkspacesPassed(options)) {
        throw new WizError_1.WizError('OPTION_NOT_SPECIFIED', '--from <workspace...>');
    }
    if (!isMonorepo && validateMonorepo_1.areWorkspacesPassed(options)) {
        throw new WizError_1.WizError('OPTION_SUPPORTED_ONLY_IN_MONOREPO', '--from <workspace...>');
    }
}
function runCreateLayoutAction(baseDir, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const layoutOptions = getLayoutOptions(baseDir, options);
        const action = yield create_1.createAction(baseDir);
        return action(baseDir, layoutOptions);
    });
}
function getLayoutOptions(baseDir, options) {
    const { layout, name, target, theme, scheme } = options;
    return {
        archive: true,
        archiveDest: baseDir,
        command: 'layout',
        from: layout,
        name: layout.length > 1 ? undefined : name,
        target,
        theme,
        scheme,
    };
}
//# sourceMappingURL=wiz-archive.js.map