import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import get from "lodash/get.js";
import EmptyBlocksWrapper from "../../../../components/editor/EmptyBlocksWrapper/EmptyBlocksWrapper.js";
import { InnerContext } from "../../../../context/innerContext/index.js";
import { getBlockKey } from "../../../../utils/index.js";
import { ConstructorBlock } from "../ConstructorBlock/ConstructorBlock.js";
import { ConstructorItem } from "../ConstructorItem/index.js";
import { ConstructorLoadable } from "../ConstructorLoadable/index.js";
export const ConstructorBlocks = ({ items }) => {
    const { loadables, shouldRenderBlock, blocks } = React.useContext(InnerContext);
    const renderer = (parentId = '', withoutConstructorBlockWrapper = false, item, index) => {
        const blockData = blocks.find(({ type }) => item.type === type);
        if (!blockData) {
            return null;
        }
        let itemElement;
        const key = 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 = get(loadables, source);
            if (!config) {
                return null;
            }
            itemElement = (_jsx(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 = (_jsx(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
        _jsx(ConstructorBlock, { data: item, index: index, children: itemElement }, blockId));
    };
    // Показываем EmptyBlocksWrapper когда нет блоков (только в режиме редактора)
    if (items.length === 0) {
        return _jsx(EmptyBlocksWrapper, {});
    }
    return _jsx(React.Fragment, { children: items.map(renderer.bind(null, '', false)) });
};
//# sourceMappingURL=ConstructorBlocks.js.map
