"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 constructor_items_1 = require("../constructor-items.js");
const schema_1 = require("../schema/index.js");
const form_generator_1 = require("../utils/form-generator.js");
const usePCEditorStore_1 = require("./usePCEditorStore.js");
const usePostMessageAPI_1 = require("./usePostMessageAPI.js");
const usePCEditorInitializeEvents = ({ initialContent, setContent, }) => {
    const { manipulateOverlayMode, 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: Object.entries(constructor_items_1.blockDataMap).reduce((acc, [key, value]) => {
                acc.push({ type: key, schema: value.schema });
                return acc;
            }, []),
            subBlocks: [],
            global: (0, form_generator_1.generateFromAJV)(schema_1.defaultComponentsConfigurationSchema),
        });
    });
    const onResize = React.useCallback(() => {
        const height = document.documentElement.getBoundingClientRect().height;
        (0, usePostMessageAPI_1.sendEventPostMessage)('ON_RESIZE', { height });
    }, []);
    new ResizeObserver(onResize).observe(document.body);
    React.useEffect(() => {
        const onMouseUp = () => {
            (0, usePostMessageAPI_1.sendEventPostMessage)('ON_MOUSE_UP', {});
        };
        const onMouseMove = (event) => {
            event.preventDefault();
            event.stopPropagation();
            (0, usePostMessageAPI_1.sendEventPostMessage)('ON_MOUSE_MOVE', { x: event.clientX, y: event.clientY });
        };
        const throttleOnMouseMove = lodash_1.default.throttle(onMouseMove, 10);
        const throttleOnMouseUp = lodash_1.default.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;
        (0, usePostMessageAPI_1.sendEventPostMessage)('ON_INIT', { height });
    }, []);
};
exports.usePCEditorInitializeEvents = usePCEditorInitializeEvents;
//# sourceMappingURL=usePCEditorInitializeEvents.js.map
