"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 chalk_1 = require("chalk");
const listr2_1 = require("listr2");
const settings_1 = require("@ewizardjs/settings");
const system_settings_1 = require("@ewizardjs/system-settings");
const ensureAuth_1 = require("./utils/ensureAuth");
const Logger_1 = require("../modules/Logger");
const I18n_1 = require("../modules/I18n");
const utils_1 = require("./../utils");
const configureHelpOptions_1 = require("../utils/configureHelpOptions");
const getThumbsScreenshoter_1 = require("./utils/getThumbsScreenshoter");
const thumbnailErrorsHandler_1 = require("./utils/thumbnailErrorsHandler");
const logger_1 = require("./utils/logger");
commander
    .name('wiz thumbs')
    .description('Generate thumbnails for e-Detailer slides or site pages. To verify thumbnails are up-to-date, run "wiz dev" before "wiz thumbs". Re-run these commands every time you change your project')
    .option('-s, --slides <slides>', 'DEPRECATED: generate thumbnails for given slides')
    .option('-i, --id <id...>', 'Generate thumbnails for e-Detailer slides or site pages. To ensure that thumbnails are up-to-date, run "wiz dev" before "wiz thumbs". Re-run these commands every time you change your project.')
    .option('-d, --delay <delay>', 'delay before screenshot in ms')
    .configureHelp({
    optionDescription: configureHelpOptions_1.capitalizeOptionDescription,
})
    .parse(process.argv);
thumbnails(commander.opts(), process.cwd())
    .catch(logger_1.handleError)
    .finally(logger_1.closeLogger);
function thumbnails(opts, presentationPath) {
    return __awaiter(this, void 0, void 0, function* () {
        yield ensureAuth_1.default();
        if (yield utils_1.ensureDevBuild(presentationPath)) {
            Logger_1.default.info(chalk_1.default.yellow(I18n_1.default.info('THUMBNAILS_REMINDER')));
        }
        const options = {
            root: presentationPath,
            settings: yield settings_1.getRaw(presentationPath),
            disableLogs: true,
        };
        if (opts.slides)
            options.requiredIds = opts.slides.split(/\s/);
        if (opts.id)
            options.requiredIds = opts.id;
        if (opts.delay)
            options.delay = parseInt(opts.delay, 10);
        const contentType = yield system_settings_1.getByPath(presentationPath, 'content.type');
        const thumbsScreenshoter = yield getThumbsScreenshoter_1.getThumbsScreenshoter(options, contentType);
        const listrTask = {
            title: I18n_1.default.info('THUMBNAILS_GENERATING'),
            task: () => __awaiter(this, void 0, void 0, function* () { return yield thumbsScreenshoter(); }),
        };
        const listrOptions = {
            rendererOptions: {
                showErrorMessage: !options.disableLogs,
            },
        };
        const task = new listr2_1.Listr(listrTask, listrOptions);
        yield task
            .run()
            .then(() => Logger_1.default.info(I18n_1.default.info('THUMBNAILS_READY')))
            .catch((err) => thumbnailErrorsHandler_1.thumbnailErrorsHandler(err, contentType));
    });
}
//# sourceMappingURL=wiz-thumbs.js.map