import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import RootCn from "../../components/RootCn/index.js";
import { navItemMap } from "../../constructor-items.js";
import { BlockRegistryContext, useBlockRegistryProvider } from "../../context/blockRegistryContext/index.js";
import { BlocksContext } from "../../context/blocksContext/index.js";
import { InnerContext } from "../../context/innerContext/index.js";
import { usePCEditorInitializeEvents } from "../../hooks/usePCEditorInitializeEvents.js";
import { usePCEditorStore } from "../../hooks/usePCEditorStore.js";
import { block as cnBlock, getCustomItems } from "../../utils/index.js";
import { ConstructorBlocks } from "./components/index.js";
import { ConstructorRow } from "./components/ConstructorRow/index.js";
import './PageConstructor.css';
const b = cnBlock('page-constructor');
export 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);
    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 = usePCEditorStore();
    const { initialized } = store;
    const blockRegistry = 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]);
    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: {
            ...navItemMap,
            ...getCustomItems(['navigation'], custom),
        },
        loadables: custom?.loadable,
        shouldRenderBlock,
        blockWrappers,
        content,
        setContent,
    }), [custom, shouldRenderBlock, availableBlocks, content, setContent, blockWrappers]);
    const restBlocks = content.blocks;
    const blocksContent = restBlocks && (_jsx(ConstructorRow, { children: _jsx(ConstructorBlocks, { items: restBlocks }) }));
    // Apply extensions (wrappers) from outermost to innermost
    const wrappedContent = extensions.reduceRight((children, extension) => extension.settings.ContentWrapper ? (_jsx(extension.settings.ContentWrapper, { ...(extension.settings.contentWrapperProps || {}), children: children })) : (children), blocksContent || null);
    return (_jsx(BlockRegistryContext.Provider, { value: blockRegistry, children: _jsx(InnerContext.Provider, { value: context, children: _jsx(RootCn, { className: b('', { ['with-editor']: initialized }), children: wrappedContent }) }) }));
};
//# sourceMappingURL=PageConstructor.js.map
