"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.handleScreenBlockCommands = exports.screenBlocks = void 0;
const blocks_renderer_1 = require("@ewizardjs/blocks-renderer");
const settings_1 = require("@ewizardjs/settings");
const utils_1 = require("./utils");
const optionBlockMakeScreen_1 = require("./optionBlockMakeScreen");
const ramda_1 = require("ramda");
const Logger_1 = require("./../../modules/Logger");
const requirePuppeteer_1 = require("../../bin/utils/requirePuppeteer");
const I18n_1 = require("../../modules/I18n");
const WARNING_THEMES_NOT_EXIST = 'Current email content need fix for use block screenshot with different themes need added configured file - themes.json in themes folder';
function screenBlocks(opts, presentationPath) {
    return __awaiter(this, void 0, void 0, function* () {
        const { clm } = opts;
        const { blocksPath, currentTheme, currentClm, themesList, settings, } = yield optionBlockMakeScreen_1.getDataForScreen(presentationPath, clm);
        const { serverPort, server } = yield utils_1.createServer(presentationPath, settings);
        const url = `http://localhost:${serverPort}`;
        const puppeteer = blocks_renderer_1.FacadePuppeteer.create();
        Logger_1.default.info(I18n_1.default.info('SCREENSHOTER_LOADING'));
        yield puppeteer.createPage(url, requirePuppeteer_1.requirePuppeteer, { waitUntil: 'networkidle0' });
        Logger_1.default.info(I18n_1.default.info('SCREENSHOTER_LOADED'));
        yield handleScreenBlockCommands({
            presentationPath,
            blocksPath,
            opts,
            currentTheme,
            currentClm,
            themesList,
            server,
            puppeteer,
        });
    });
}
exports.screenBlocks = screenBlocks;
function handleScreenBlockCommands(screenOptions) {
    return __awaiter(this, void 0, void 0, function* () {
        const { opts, server, themesList, puppeteer, } = screenOptions;
        const themesWarningOptions = {
            condition: !themesList.length,
            message: WARNING_THEMES_NOT_EXIST,
            puppeteer,
            server,
        };
        const { all, id, theme, clm } = opts;
        const COMMAND_WITHOUT_OPTIONS = ramda_1.isEmpty(opts);
        const CLMs = Object.keys(settings_1.TargetCLMs);
        switch (true) {
            case (COMMAND_WITHOUT_OPTIONS): {
                Logger_1.default.info('Generating icon screenshots for all blocks for the current theme and current target system (targetCLM)...');
                yield optionBlockMakeScreen_1.initMakeScreenForBlocks(Object.assign(Object.assign({}, screenOptions), { opts: {} }));
                break;
            }
            case (!!(id && theme && clm)): {
                Logger_1.default.info('Generating icon screenshots for one block, one theme, and one target system (targetCLM)...');
                yield optionBlockMakeScreen_1.initMakeScreenForBlock(Object.assign(Object.assign({}, screenOptions), { opts, currentTheme: theme, currentClm: clm }));
                break;
            }
            case (!!(id && theme)): {
                Logger_1.default.info('Generating icon screenshots for one block, one theme, and all target systems (targetCLM)...');
                for (const currentClm of CLMs) {
                    yield optionBlockMakeScreen_1.initMakeScreenForBlock(Object.assign(Object.assign({}, screenOptions), { opts: { id, clm: currentClm, theme }, currentTheme: theme, currentClm }));
                }
                break;
            }
            case (!!(id && clm)): {
                const condition = yield utils_1.sendWarningAndCancelCommand(themesWarningOptions);
                if (condition)
                    return;
                Logger_1.default.info('Generating icon screenshots for one block, all themes, and one target system (targetCLM)...');
                for (const { id: currentTheme } of themesList) {
                    yield optionBlockMakeScreen_1.initMakeScreenForBlock(Object.assign(Object.assign({}, screenOptions), { opts: { id, clm, theme: currentTheme }, currentTheme, currentClm: clm }));
                }
                break;
            }
            case (!!(id)): {
                const condition = yield utils_1.sendWarningAndCancelCommand(themesWarningOptions);
                if (condition)
                    return;
                Logger_1.default.info('Generating icon screenshots for one block, all themes, and all target systems (targetCLM)...');
                for (const { id: currentTheme } of themesList) {
                    for (const currentClm of CLMs) {
                        yield optionBlockMakeScreen_1.initMakeScreenForBlock(Object.assign(Object.assign({}, screenOptions), { opts: { id, clm: currentClm, theme: currentTheme }, currentTheme, currentClm }));
                    }
                }
                break;
            }
            case (!!(theme && clm)): {
                Logger_1.default.info('Generating icon screenshots for all blocks, one theme, and one target system (targetCLM)...');
                yield optionBlockMakeScreen_1.initMakeScreenForBlocks(Object.assign(Object.assign({}, screenOptions), { opts, currentTheme: theme, currentClm: clm }));
                break;
            }
            case (!!(theme)): {
                Logger_1.default.info('Generating icon screenshots for all blocks, one theme, and all target systems (targetCLM)...');
                for (const currentClm of CLMs) {
                    yield optionBlockMakeScreen_1.initMakeScreenForBlocks(Object.assign(Object.assign({}, screenOptions), { opts: { theme, clm: currentClm }, currentTheme: theme, currentClm }));
                }
                break;
            }
            case (!!(clm)): {
                const condition = yield utils_1.sendWarningAndCancelCommand(themesWarningOptions);
                if (condition)
                    return;
                Logger_1.default.info('Generating icon screenshots for all blocks, all themes, and one target system (targetCLM)...');
                for (const { id: currentTheme } of themesList) {
                    yield optionBlockMakeScreen_1.initMakeScreenForBlocks(Object.assign(Object.assign({}, screenOptions), { opts: { clm, theme: currentTheme }, currentTheme, currentClm: clm }));
                }
                break;
            }
            case (all): {
                const condition = yield utils_1.sendWarningAndCancelCommand(themesWarningOptions);
                if (condition)
                    return;
                Logger_1.default.info('Generating icon screenshots for all blocks, all themes, and all target systems (targetCLM)...');
                for (const { id: currentTheme } of themesList) {
                    for (const currentClm of CLMs) {
                        yield optionBlockMakeScreen_1.initMakeScreenForBlocks(Object.assign(Object.assign({}, screenOptions), { opts: { clm: currentClm, theme: currentTheme }, currentTheme, currentClm }));
                    }
                }
                break;
            }
            default:
                Logger_1.default.info('Unresolveed option(s)');
        }
        yield utils_1.destroyBrowserAndServer(puppeteer, server);
    });
}
exports.handleScreenBlockCommands = handleScreenBlockCommands;
//# sourceMappingURL=optionBlock.js.map