"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConstructorBlocks = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const get_1 = tslib_1.__importDefault(require("lodash/get.js"));
const EmptyBlocksWrapper_1 = tslib_1.__importDefault(require("../../../../components/editor/EmptyBlocksWrapper/EmptyBlocksWrapper.js"));
const innerContext_1 = require("../../../../context/innerContext/index.js");
const utils_1 = require("../../../../utils/index.js");
const ConstructorBlock_1 = require("../ConstructorBlock/ConstructorBlock.js");
const ConstructorItem_1 = require("../ConstructorItem/index.js");
const ConstructorLoadable_1 = require("../ConstructorLoadable/index.js");
const ConstructorBlocks = ({ items }) => {
    const { loadables, shouldRenderBlock, blocks } = React.useContext(innerContext_1.InnerContext);
    const renderer = (parentId = '', withoutConstructorBlockWrapper = false, item, index) => {
        const blockData = blocks.find(({ type }) => item.type === type);
        if (!blockData) {
            return null;
        }
        let itemElement;
        const key = (0, utils_1.getBlockKey)(item, index);
        const blockId = parentId ? `${parentId}_${key}` : key;
        if (shouldRenderBlock && !shouldRenderBlock(item, blockId)) {
            return null;
        }
        if ('loadable' in item && item.loadable) {
            const { source, serviceId, params } = item.loadable;
            const config = (0, get_1.default)(loadables, source);
            if (!config) {
                return null;
            }
            itemElement = ((0, jsx_runtime_1.jsx)(ConstructorLoadable_1.ConstructorLoadable, { block: item, blockKey: blockId, config: config, serviceId: serviceId, params: params }, blockId));
        }
        else {
            let children;
            if ('children' in item && item.children) {
                children = item.children.map(renderer.bind(null, blockId, true));
            }
            itemElement = ((0, jsx_runtime_1.jsx)(ConstructorItem_1.ConstructorItem, { data: item, blockKey: index, children: children }, blockId));
        }
        return withoutConstructorBlockWrapper ? (itemElement) : (
        //TODO: replace ConstructorBlock (and delete it) with BlockBase when all
        // components relying on constructor inner structure like Slider or blog-constructor will be refactored
        (0, jsx_runtime_1.jsx)(ConstructorBlock_1.ConstructorBlock, { data: item, index: index, children: itemElement }, blockId));
    };
    // Показываем EmptyBlocksWrapper когда нет блоков (только в режиме редактора)
    if (items.length === 0) {
        return (0, jsx_runtime_1.jsx)(EmptyBlocksWrapper_1.default, {});
    }
    return (0, jsx_runtime_1.jsx)(React.Fragment, { children: items.map(renderer.bind(null, '', false)) });
};
exports.ConstructorBlocks = ConstructorBlocks;
//# sourceMappingURL=ConstructorBlocks.js.map
