"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useEditorState = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
// import {PageConstructorExtension} from '../../../../containers/PageConstructor/PageConstructor';
// import {BlockWrapperDataProps} from '../../../../models';
const schema_1 = require("../../../../gravity-blocks/schema/index.js");
// import EditBlock from '../../../components/EditBlock/EditBlock';
// import {ErrorBoundary} from '../../../components/ErrorBoundary/ErrorBoundary';
// import {NotFoundBlock} from '../../../components/NotFoundBlock/NotFoundBlock';
const useCodeValidator_1 = require("../../../hooks/useCodeValidator.js");
const main_1 = require("../../../store/main/index.js");
const settings_1 = require("../../../store/settings/index.js");
const types_1 = require("../../../types/index.js");
const utils_1 = require("../../../utils/index.js");
const useCode_1 = require("./useCode.js");
// Editor business logic
const useEditorState = ({ customSchema, onChange, providerProps, transformContent, deviceEmulationSettings, theme: editorTheme, ...rest }) => {
    const { content, activeBlockIndex, errorBoundaryState, onContentUpdate, onAdd, onSelect } = (0, main_1.useMainState)(rest);
    const { viewMode, editMode, onEditModeUpdate, theme: constructorTheme, onViewModeUpdate, onThemeUpdate, codeFullscreeModeOn, onCodeFullscreeModeOnUpdate, } = (0, settings_1.useSettingsState)();
    const isCodeEditMode = editMode === types_1.EditModeItem.Code;
    const isViewEditMode = editMode === types_1.EditModeItem.View;
    const isFormEditMode = editMode === types_1.EditModeItem.Form;
    const isDesktopViewMode = viewMode === types_1.ViewModeItem.Desktop;
    const isCodeOnlyMode = codeFullscreeModeOn && isCodeEditMode;
    const transformedContent = React.useMemo(() => (transformContent ? transformContent(content, { viewMode, editMode }) : content), [content, transformContent, viewMode, editMode]);
    const schema = React.useMemo(() => (0, schema_1.generateDefaultSchema)(customSchema), [customSchema]);
    const codeValidator = (0, useCodeValidator_1.useCodeValidator)(schema);
    const outgoingProps = React.useMemo(() => {
        const userExtensions = rest.extensions ?? [];
        if (isCodeEditMode || isViewEditMode) {
            return {
                content: transformedContent,
                custom: rest.custom,
                extensions: userExtensions,
                viewMode,
            };
        }
        // const editorExtensions: PageConstructorExtension[] = [
        //     {
        //         name: 'Editor Not Found Block',
        //         id: '@gravity-ui/page-constructor/editor-not-found',
        //         settings: {
        //             blockWrapper: ({
        //                 children,
        //                 type,
        //             }: BlockWrapperDataProps & React.PropsWithChildren) => (
        //                 <NotFoundBlock type={type} content={{}}>
        //                     {children}
        //                 </NotFoundBlock>
        //             ),
        //         },
        //     },
        //     {
        //         name: 'Editor Edit Block',
        //         id: '@gravity-ui/page-constructor/editor-edit-block',
        //         settings: {
        //             blockWrapper: (props: BlockWrapperDataProps & React.PropsWithChildren) => (
        //                 <EditBlock {...injectEditBlockProps(props)} />
        //             ),
        //         },
        //     },
        //     {
        //         name: 'Editor Error Boundary',
        //         id: '@gravity-ui/page-constructor/editor-error-boundary',
        //         settings: {
        //             blockWrapper: (props: BlockWrapperDataProps & React.PropsWithChildren) => (
        //                 <ErrorBoundary
        //                     {...props}
        //                     key={`${getBlockId(props)}-${errorBoundaryState}`}
        //                 />
        //             ),
        //         },
        //     },
        // ];
        return {
            content: transformedContent,
            custom: rest.custom,
            extensions: userExtensions,
            viewMode,
        };
    }, [
        viewMode,
        transformedContent,
        rest.custom,
        rest.extensions,
        isCodeEditMode,
        isViewEditMode,
    ]);
    const context = React.useMemo(() => ({
        constructorProps: {
            content: transformedContent,
            custom: rest.custom,
        },
        providerProps: {
            ...providerProps,
            isMobile: (0, utils_1.checkIsMobile)(viewMode),
            theme: constructorTheme,
        },
        deviceEmulationSettings,
        theme: editorTheme,
    }), [
        providerProps,
        rest.custom,
        viewMode,
        transformedContent,
        deviceEmulationSettings,
        constructorTheme,
        editorTheme,
    ]);
    React.useEffect(() => {
        onChange?.(content);
    }, [content, onChange]);
    const code = (0, useCode_1.useCode)({ isCodeEditMode, content, codeFullscreeModeOn });
    return {
        context,
        viewMode,
        editMode,
        constructorTheme,
        onThemeUpdate,
        onViewModeUpdate,
        onEditModeUpdate,
        isCodeEditMode,
        isDesktopViewMode,
        content,
        schema,
        onContentUpdate,
        code,
        codeValidator,
        codeFullscreeModeOn,
        onCodeFullscreeModeOnUpdate,
        activeBlockIndex,
        onSelect,
        isCodeOnlyMode,
        errorBoundaryState,
        outgoingProps,
        onAdd,
        isFormEditMode,
    };
};
exports.useEditorState = useEditorState;
//# sourceMappingURL=useEditorState.js.map
