"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.initMakeScreenForBlock = exports.getDataForScreen = exports.initMakeScreenForBlocks = void 0;
const screenUtils_1 = require("../../utils/screenUtils");
const pathUtils_1 = require("../../utils/pathUtils");
const Logger_1 = require("./../../modules/Logger");
const systemSettingsApi = require("@ewizardjs/system-settings");
const settings_1 = require("@ewizardjs/settings");
const getThemesList_1 = require("../../utils/getThemesList");
const I18n_1 = require("./../../modules/I18n");
function initMakeScreenForBlocks(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { blocks, blocksListDictionary, iconThemePartName, iconClmPartName } = yield getDataForScreenBlock(options);
        for (const block of blocks) {
            yield screenBlock(Object.assign(Object.assign({}, options), { blockName: block.name, blocksListDictionary, iconThemePartName, iconClmPartName }));
        }
        Logger_1.default.info(I18n_1.default.info('SCREEN_BLOCK_READY'));
    });
}
exports.initMakeScreenForBlocks = initMakeScreenForBlocks;
function getDataForScreen(presentationPath, clm) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        const [blocksPath, settings, currentTheme] = yield Promise.all([
            systemSettingsApi.getByPath(presentationPath, 'path.blocks'),
            settings_1.get(presentationPath, {}),
            settings_1.getCurrentTheme(presentationPath),
        ]);
        const currentClm = clm || ((_a = settings.targetCLM) === null || _a === void 0 ? void 0 : _a.code);
        const themesList = yield getThemesList_1.getThemesList(presentationPath);
        return { blocksPath, currentTheme, currentClm, themesList, settings };
    });
}
exports.getDataForScreen = getDataForScreen;
function initMakeScreenForBlock(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { blocksListDictionary, iconThemePartName, iconClmPartName } = yield getDataForScreenBlock(options);
        const { opts: { id } } = options;
        yield screenBlock(Object.assign(Object.assign({}, options), { blockName: id, blocksListDictionary, iconThemePartName, iconClmPartName }));
    });
}
exports.initMakeScreenForBlock = initMakeScreenForBlock;
function getDataForScreenBlock(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { presentationPath, opts, currentTheme, currentClm, puppeteer, } = options;
        const blockId = opts.id ? [opts.id] : [];
        const [, , { blocks, blocksListDictionary, iconThemePartName, iconClmPartName, }] = yield Promise.all([
            yield puppeteer.createRequestInterceptor(opts, 'settings.json'),
            yield puppeteer.renderBlocks(presentationPath, blockId),
            yield getDataForBlocks({ puppeteer, currentTheme, currentClm }),
        ]);
        return { blocks, blocksListDictionary, iconThemePartName, iconClmPartName };
    });
}
function getDataForBlocks(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { puppeteer, currentTheme, currentClm } = options;
        const { __blocks, __blocksList } = yield puppeteer.getWindowData(['__blocks', '__blocksList']);
        const blocksListDictionary = screenUtils_1.parsingBlocksArrayToDictionary(__blocksList);
        const { iconThemePartName, iconClmPartName } = createIconsName(currentTheme, currentClm);
        return { blocks: __blocks, blocksListDictionary, iconThemePartName, iconClmPartName };
    });
}
function createIconsName(theme, clm) {
    const iconThemePartName = theme ? `-${theme}` : '';
    const iconClmPartName = clm ? `-${clm}` : '';
    return { iconThemePartName, iconClmPartName };
}
function screenBlock(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { blockName, blocksListDictionary, puppeteer, blocksPath, iconThemePartName, iconClmPartName, presentationPath, } = options;
        const blockElement = yield puppeteer.getElement(`[data-block=${blockName}] > table`);
        if (!blockElement) {
            Logger_1.default.info(`Failed to generate icon screenshot for the ${blockName} block`);
            return;
        }
        const path = pathUtils_1.getIconLocation(presentationPath, {
            blocksPath, block: blocksListDictionary[blockName], iconThemePartName, iconClmPartName,
        });
        yield blockElement.screenshot({ path });
        Logger_1.default.info(`Block icon created in ${path}`);
    });
}
//# sourceMappingURL=optionBlockMakeScreen.js.map