"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePCEditorInitializeEvents = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const rect_1 = require("../common/types/rect.js");
const usePCEditorStore_1 = require("./usePCEditorStore.js");
const usePostMessageAPI_1 = require("./usePostMessageAPI.js");
function collectRectMap(registry) {
    return registry.getEntries().map(({ path, element, dropZone }) => {
        const rect = element.getClientRects().item(0) ?? element.getBoundingClientRect();
        return { path, rect: (0, rect_1.toSerializableRect)(rect), dropZone };
    });
}
const usePCEditorInitializeEvents = ({ initialContent, setContent, blocks, global, blockInputs, registry, }) => {
    const { initialized, content } = (0, usePCEditorStore_1.usePCEditorStore)();
    React.useEffect(() => {
        if (initialized) {
            setContent(content);
        }
    }, [content, initialized, setContent]);
    (0, usePostMessageAPI_1.useInternalPostMessageAPIListener)('GET_INITIAL_CONTENT', () => {
        (0, usePostMessageAPI_1.sendEventPostMessage)('ON_INITIAL_CONTENT', initialContent);
    });
    (0, usePostMessageAPI_1.useInternalPostMessageAPIListener)('GET_SUPPORTED_BLOCKS', () => {
        (0, usePostMessageAPI_1.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;
        (0, usePostMessageAPI_1.sendEventPostMessage)('ON_RESIZE', { height });
    }, []);
    React.useEffect(() => {
        if (!registry) {
            return undefined;
        }
        let frame = null;
        const sendRectMap = () => {
            frame = null;
            (0, usePostMessageAPI_1.sendEventPostMessage)('ON_UPDATE_RECT_MAP', { rects: collectRectMap(registry) });
        };
        const scheduleSend = () => {
            if (frame !== null) {
                return;
            }
            frame = requestAnimationFrame(sendRectMap);
        };
        const throttledSchedule = lodash_1.default.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;
        (0, usePostMessageAPI_1.sendEventPostMessage)('ON_INIT', { height });
    }, []);
};
exports.usePCEditorInitializeEvents = usePCEditorInitializeEvents;
//# sourceMappingURL=usePCEditorInitializeEvents.js.map
