import * as React from 'react';
import _ from 'lodash';
import { toSerializableRect } from "../common/types/rect.js";
import { usePCEditorStore } from "./usePCEditorStore.js";
import { sendEventPostMessage, useInternalPostMessageAPIListener } from "./usePostMessageAPI.js";
function collectRectMap(registry) {
    return registry.getEntries().map(({ path, element, dropZone }) => {
        const rect = element.getClientRects().item(0) ?? element.getBoundingClientRect();
        return { path, rect: toSerializableRect(rect), dropZone };
    });
}
export const usePCEditorInitializeEvents = ({ initialContent, setContent, blocks, global, blockInputs, registry, }) => {
    const { 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: blocks.map((block) => ({
                type: block.type,
                schema: blockInputs?.length
                    ? {
                        ...block.schema,
                        inputs: [...blockInputs, ...(block.schema?.inputs || [])],
                    }
                    : block.schema,
            })),
            subBlocks: [],
            global: global || [],
        });
    });
    const onResize = React.useCallback(() => {
        const height = document.body.scrollHeight;
        sendEventPostMessage('ON_RESIZE', { height });
    }, []);
    React.useEffect(() => {
        if (!registry) {
            return undefined;
        }
        let frame = null;
        const sendRectMap = () => {
            frame = null;
            sendEventPostMessage('ON_UPDATE_RECT_MAP', { rects: collectRectMap(registry) });
        };
        const scheduleSend = () => {
            if (frame !== null) {
                return;
            }
            frame = requestAnimationFrame(sendRectMap);
        };
        const throttledSchedule = _.throttle(scheduleSend, 100, { leading: true, trailing: true });
        const unsubscribe = registry.subscribe(throttledSchedule);
        const observer = new ResizeObserver(throttledSchedule);
        observer.observe(document.body);
        // Initial push once registry is ready.
        throttledSchedule();
        return () => {
            throttledSchedule.cancel();
            unsubscribe();
            observer.disconnect();
            if (frame !== null) {
                cancelAnimationFrame(frame);
            }
        };
    }, [registry]);
    React.useEffect(() => {
        window.addEventListener('resize', onResize);
        const observer = new ResizeObserver(onResize);
        observer.observe(document.documentElement);
        observer.observe(document.body);
        return () => {
            window.removeEventListener('resize', onResize);
            observer.disconnect();
        };
    }, [onResize]);
    React.useEffect(() => {
        const height = document.body.scrollHeight;
        sendEventPostMessage('ON_INIT', { height });
    }, []);
};
//# sourceMappingURL=usePCEditorInitializeEvents.js.map
