"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageConstructor = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const RootCn_1 = tslib_1.__importDefault(require("../../components/RootCn/index.js"));
const constructor_items_1 = require("../../constructor-items.js");
const blockRegistryContext_1 = require("../../context/blockRegistryContext/index.js");
const blocksContext_1 = require("../../context/blocksContext/index.js");
const innerContext_1 = require("../../context/innerContext/index.js");
const usePCEditorInitializeEvents_1 = require("../../hooks/usePCEditorInitializeEvents.js");
const usePCEditorStore_1 = require("../../hooks/usePCEditorStore.js");
const utils_1 = require("../../utils/index.js");
const components_1 = require("./components/index.js");
const ConstructorRow_1 = require("./components/ConstructorRow/index.js");
const b = (0, utils_1.block)('page-constructor');
const PageConstructor = (props) => {
    const { content: initialContent = { blocks: [] }, shouldRenderBlock, custom, blocks: availableLocalBlocks = [], extensions: extensionsProp, } = props;
    const extensions = React.useMemo(() => extensionsProp ?? [], [extensionsProp]);
    const { blocks: availableGlobalBlocks } = React.useContext(blocksContext_1.BlocksContext);
    const availableBlocks = React.useMemo(() => [...availableGlobalBlocks, ...availableLocalBlocks], [availableGlobalBlocks, availableLocalBlocks]);
    const globalDefaults = extensions.reduce((acc, extension) => ({
        ...acc,
        ...(extension.settings.globalDefaults || {}),
    }), {});
    const [content, setContent] = React.useState({
        ...globalDefaults,
        ...initialContent,
    });
    const store = (0, usePCEditorStore_1.usePCEditorStore)();
    const { initialized } = store;
    const blockRegistry = (0, blockRegistryContext_1.useBlockRegistryProvider)();
    const blockWrappers = React.useMemo(() => extensions.flatMap((ext) => ext.settings.blockWrapper
        ? [
            {
                wrapper: ext.settings.blockWrapper,
                props: ext.settings.blockWrapperProps ?? {},
            },
        ]
        : []), [extensions]);
    const blockInputs = React.useMemo(() => extensions.reduce((acc, ext) => [...acc, ...(ext.settings.blockInputs || [])], []), [extensions]);
    (0, usePCEditorInitializeEvents_1.usePCEditorInitializeEvents)({
        initialContent: content,
        setContent,
        blocks: availableBlocks,
        global: extensions.reduce((acc, extension) => [...acc, ...(extension.settings.globalInputs || [])], []),
        blockInputs,
        registry: blockRegistry,
    });
    const context = React.useMemo(() => ({
        blocks: availableBlocks,
        navItemMap: {
            ...constructor_items_1.navItemMap,
            ...(0, utils_1.getCustomItems)(['navigation'], custom),
        },
        loadables: custom?.loadable,
        shouldRenderBlock,
        blockWrappers,
        content,
        setContent,
    }), [custom, shouldRenderBlock, availableBlocks, content, setContent, blockWrappers]);
    const restBlocks = content.blocks;
    const blocksContent = restBlocks && ((0, jsx_runtime_1.jsx)(ConstructorRow_1.ConstructorRow, { children: (0, jsx_runtime_1.jsx)(components_1.ConstructorBlocks, { items: restBlocks }) }));
    // Apply extensions (wrappers) from outermost to innermost
    const wrappedContent = extensions.reduceRight((children, extension) => extension.settings.ContentWrapper ? ((0, jsx_runtime_1.jsx)(extension.settings.ContentWrapper, { ...(extension.settings.contentWrapperProps || {}), children: children })) : (children), blocksContent || null);
    return ((0, jsx_runtime_1.jsx)(blockRegistryContext_1.BlockRegistryContext.Provider, { value: blockRegistry, children: (0, jsx_runtime_1.jsx)(innerContext_1.InnerContext.Provider, { value: context, children: (0, jsx_runtime_1.jsx)(RootCn_1.default, { className: b('', { ['with-editor']: initialized }), children: wrappedContent }) }) }));
};
exports.PageConstructor = PageConstructor;
//# sourceMappingURL=PageConstructor.js.map
