"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const postMessage_1 = require("../../common/postMessage.js");
const useMainEditorStore_1 = require("./useMainEditorStore.js");
const usePostMessageEvents_1 = require("./usePostMessageEvents.js");
const useMainEditorInitialize = (initialContent) => {
    const { requestPostMessage } = (0, usePostMessageEvents_1.usePostMessageEvents)();
    const { initialize, setConfig, setContent, manipulateOverlayMode, disableMode, insertBlock, reorderBlock, preInsertBlockType, preReorderBlockPath, } = (0, useMainEditorStore_1.useMainEditorStore)();
    (0, postMessage_1.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]);
    (0, postMessage_1.usePostMessageAPIListener)('ON_INITIAL_CONTENT', (data) => {
        setContent(data);
    });
    (0, postMessage_1.usePostMessageAPIListener)('ON_SUPPORTED_BLOCKS', (data) => {
        setConfig(data);
    });
    (0, postMessage_1.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]);
};
exports.default = useMainEditorInitialize;
//# sourceMappingURL=useMainEditorInitialize.js.map
