import * as React from 'react';
import { usePostMessageAPIListener } from "../../common/postMessage.js";
import { useMainEditorStore } from "./useMainEditorStore.js";
import { usePostMessageEvents } from "./usePostMessageEvents.js";
const useMainEditorInitialize = (initialContent) => {
    const { requestPostMessage } = usePostMessageEvents();
    const { initialize, setConfig, setContent, manipulateOverlayMode, disableMode, insertBlock, reorderBlock, preInsertBlockType, preReorderBlockPath, } = useMainEditorStore();
    usePostMessageAPIListener('ON_INIT', () => {
        initialize();
        requestPostMessage('GET_SUPPORTED_BLOCKS', {});
        if (!initialContent) {
            requestPostMessage('GET_INITIAL_CONTENT', {});
        }
    }, [requestPostMessage]);
    React.useEffect(() => {
        if (initialContent) {
            setContent(initialContent);
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [initialContent]);
    usePostMessageAPIListener('ON_INITIAL_CONTENT', (data) => {
        setContent(data);
    });
    usePostMessageAPIListener('ON_SUPPORTED_BLOCKS', (data) => {
        setConfig(data);
    });
    usePostMessageAPIListener('ON_MOUSE_UP', ({ path, position }) => {
        if (manipulateOverlayMode === 'insert' && path && position && preInsertBlockType) {
            insertBlock(path, preInsertBlockType, ['left', 'top'].includes(position) ? 'prepend' : 'append');
        }
        if (manipulateOverlayMode === 'reorder' && path && position && preReorderBlockPath) {
            reorderBlock(preReorderBlockPath, path, ['left', 'top'].includes(position) ? 'prepend' : 'append');
        }
        disableMode();
    }, [preInsertBlockType, preReorderBlockPath]);
};
export default useMainEditorInitialize;
//# sourceMappingURL=useMainEditorInitialize.js.map
