import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { removeFn } from "../../../common/utils.js";
import { createEditorStore } from "../../store.js";
import { IframeContext } from "../iframeContext/index.js";
import { MainEditorStoreContext } from "./MainEditorStoreContext.js";
export const MainEditorStoreProvider = ({ children }) => {
    const { iframeElement } = React.useContext(IframeContext);
    const storeRef = React.useRef();
    const sendPostMessage = React.useCallback((data) => {
        const message = {
            state: data,
        };
        if (iframeElement && iframeElement.contentWindow) {
            iframeElement.contentWindow.postMessage(message, '*');
        }
    }, [iframeElement]);
    if (!storeRef.current) {
        storeRef.current = createEditorStore();
    }
    React.useEffect(() => {
        storeRef.current?.subscribe((state) => {
            sendPostMessage(removeFn(state));
        });
    }, [sendPostMessage]);
    return (_jsx(MainEditorStoreContext.Provider, { value: {
            state: storeRef.current,
        }, children: children }));
};
//# sourceMappingURL=MainEditorStoreProvider.js.map
