import * as React from 'react';
import _ from 'lodash';
import { blockDataMap } from "../constructor-items.js";
import { defaultComponentsConfigurationSchema } from "../schema/index.js";
import { generateFromAJV } from "../utils/form-generator.js";
import { usePCEditorStore } from "./usePCEditorStore.js";
import { sendEventPostMessage, useInternalPostMessageAPIListener } from "./usePostMessageAPI.js";
export const usePCEditorInitializeEvents = ({ initialContent, setContent, }) => {
    const { manipulateOverlayMode, initialized, content } = usePCEditorStore();
    React.useEffect(() => {
        if (initialized) {
            setContent(content);
        }
    }, [content, initialized, setContent]);
    useInternalPostMessageAPIListener('GET_INITIAL_CONTENT', () => {
        sendEventPostMessage('ON_INITIAL_CONTENT', initialContent);
    });
    useInternalPostMessageAPIListener('GET_SUPPORTED_BLOCKS', () => {
        sendEventPostMessage('ON_SUPPORTED_BLOCKS', {
            blocks: Object.entries(blockDataMap).reduce((acc, [key, value]) => {
                acc.push({ type: key, schema: value.schema });
                return acc;
            }, []),
            subBlocks: [],
            global: generateFromAJV(defaultComponentsConfigurationSchema),
        });
    });
    const onResize = React.useCallback(() => {
        const height = document.documentElement.getBoundingClientRect().height;
        sendEventPostMessage('ON_RESIZE', { height });
    }, []);
    new ResizeObserver(onResize).observe(document.body);
    React.useEffect(() => {
        const onMouseUp = () => {
            sendEventPostMessage('ON_MOUSE_UP', {});
        };
        const onMouseMove = (event) => {
            event.preventDefault();
            event.stopPropagation();
            sendEventPostMessage('ON_MOUSE_MOVE', { x: event.clientX, y: event.clientY });
        };
        const throttleOnMouseMove = _.throttle(onMouseMove, 10);
        const throttleOnMouseUp = _.throttle(onMouseUp, 10);
        document.addEventListener('mousemove', throttleOnMouseMove);
        document.addEventListener('mouseup', throttleOnMouseUp);
        window.addEventListener('resize', onResize);
        return () => {
            document.removeEventListener('mousemove', throttleOnMouseMove);
            document.removeEventListener('mouseup', throttleOnMouseUp);
            window.removeEventListener('resize', onResize);
        };
    }, [manipulateOverlayMode, onResize]);
    React.useEffect(() => {
        const height = document.documentElement.getBoundingClientRect().height;
        sendEventPostMessage('ON_INIT', { height });
    }, []);
};
//# sourceMappingURL=usePCEditorInitializeEvents.js.map
