"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, rendererOptions } = opts;
        const { blocksPath, currentTheme, currentClm, themesList, settings, } = yield (0, optionBlockMakeScreen_1.getDataForScreen)(presentationPath, clm);
        const { serverPort, server } = yield (0, 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' }, rendererOptions);
        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 = (0, ramda_1.isEmpty)(opts);
        const CLMs = Object.keys(settings_1.TargetCLMs);
        switch (true) {
            case (COMMAND_WITHOUT_OPTIONS): {
                yield handleCommandWithoutOptions(screenOptions);
                break;
            }
            case (!!(id && theme && clm)): {
                yield handleNoThemeAndIdAndClm(screenOptions, opts, theme, clm);
                break;
            }
            case (!!(id && theme)): {
                yield handleNoIdAndTheme(CLMs, screenOptions, id, theme);
                break;
            }
            case (!!(id && clm)): {
                yield handleNoIdAndClm(themesWarningOptions, themesList, screenOptions, id, clm);
                break;
            }
            case (!!(id)): {
                yield handleNoId(id, themesWarningOptions, themesList, screenOptions, CLMs);
                break;
            }
            case (!!(theme && clm)): {
                yield handleNoThemeAndClm(screenOptions, opts, theme, clm);
                break;
            }
            case (!!(theme)): {
                yield handleNoTheme(CLMs, screenOptions, theme);
                break;
            }
            case (!!(clm)): {
                yield handleNoClm(themesWarningOptions, themesList, screenOptions, clm);
                break;
            }
            case (all): {
                yield handleAll(themesWarningOptions, themesList, screenOptions, CLMs);
                break;
            }
            default:
                Logger_1.default.info('Unresolveed option(s)');
        }
        yield (0, utils_1.destroyBrowserAndServer)(puppeteer, server);
    });
}
exports.handleScreenBlockCommands = handleScreenBlockCommands;
function handleNoTheme(CLMs, screenOptions, theme) {
    return __awaiter(this, void 0, void 0, function* () {
        Logger_1.default.info('Generating icon screenshots for all blocks, one theme, and all target systems (targetCLM)...');
        for (const currentClm of CLMs) {
            yield (0, optionBlockMakeScreen_1.initMakeScreenForBlocks)(Object.assign(Object.assign({}, screenOptions), { opts: { theme, clm: currentClm }, currentTheme: theme, currentClm }));
        }
    });
}
function handleNoThemeAndClm(screenOptions, opts, theme, clm) {
    return __awaiter(this, void 0, void 0, function* () {
        Logger_1.default.info('Generating icon screenshots for all blocks, one theme, and one target system (targetCLM)...');
        yield (0, optionBlockMakeScreen_1.initMakeScreenForBlocks)(Object.assign(Object.assign({}, screenOptions), { opts, currentTheme: theme, currentClm: clm }));
    });
}
function handleNoIdAndTheme(CLMs, screenOptions, id, theme) {
    return __awaiter(this, void 0, void 0, function* () {
        Logger_1.default.info('Generating icon screenshots for one block, one theme, and all target systems (targetCLM)...');
        for (const currentClm of CLMs) {
            yield (0, optionBlockMakeScreen_1.initMakeScreenForBlock)(Object.assign(Object.assign({}, screenOptions), { opts: { id, clm: currentClm, theme }, currentTheme: theme, currentClm }));
        }
    });
}
function handleNoThemeAndIdAndClm(screenOptions, opts, theme, clm) {
    return __awaiter(this, void 0, void 0, function* () {
        Logger_1.default.info('Generating icon screenshots for one block, one theme, and one target system (targetCLM)...');
        yield (0, optionBlockMakeScreen_1.initMakeScreenForBlock)(Object.assign(Object.assign({}, screenOptions), { opts, currentTheme: theme, currentClm: clm }));
    });
}
function handleCommandWithoutOptions(screenOptions) {
    return __awaiter(this, void 0, void 0, function* () {
        Logger_1.default.info('Generating icon screenshots for all blocks for the current theme and current target system (targetCLM)...');
        yield (0, optionBlockMakeScreen_1.initMakeScreenForBlocks)(Object.assign(Object.assign({}, screenOptions), { opts: {} }));
    });
}
function handleNoIdAndClm(themesWarningOptions, themesList, screenOptions, id, clm) {
    return __awaiter(this, void 0, void 0, function* () {
        const condition = yield (0, 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 (0, optionBlockMakeScreen_1.initMakeScreenForBlock)(Object.assign(Object.assign({}, screenOptions), { opts: { id, clm, theme: currentTheme }, currentTheme, currentClm: clm }));
        }
    });
}
function handleNoId(id, themesWarningOptions, themesList, screenOptions, CLMs) {
    return __awaiter(this, void 0, void 0, function* () {
        const condition = yield (0, 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 (0, optionBlockMakeScreen_1.initMakeScreenForBlock)(Object.assign(Object.assign({}, screenOptions), { opts: { id, clm: currentClm, theme: currentTheme }, currentTheme, currentClm }));
            }
        }
    });
}
function handleNoClm(themesWarningOptions, themesList, screenOptions, clm) {
    return __awaiter(this, void 0, void 0, function* () {
        const condition = yield (0, 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 (0, optionBlockMakeScreen_1.initMakeScreenForBlocks)(Object.assign(Object.assign({}, screenOptions), { opts: { clm, theme: currentTheme }, currentTheme, currentClm: clm }));
        }
    });
}
function handleAll(themesWarningOptions, themesList, screenOptions, CLMs) {
    return __awaiter(this, void 0, void 0, function* () {
        const condition = yield (0, 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 (0, optionBlockMakeScreen_1.initMakeScreenForBlocks)(Object.assign(Object.assign({}, screenOptions), { opts: { clm: currentClm, theme: currentTheme }, currentTheme, currentClm }));
            }
        }
    });
}
//# sourceMappingURL=optionBlock.js.map