import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { POST_MESSAGE_SOURCE } from "../../../common/constants.js";
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,
            source: POST_MESSAGE_SOURCE,
        };
        if (iframeElement && iframeElement.contentWindow) {
            iframeElement.contentWindow.postMessage(message, '*');
        }
    }, [iframeElement]);
    if (!storeRef.current) {
        storeRef.current = createEditorStore();
    }
    React.useEffect(() => {
        storeRef.current?.subscribe((state) => {
            const { historyPast: _historyPast, historyFuture: _historyFuture, ...syncable } = state;
            sendPostMessage(removeFn({ ...syncable, historyPast: [], historyFuture: [] }));
        });
    }, [sendPostMessage]);
    return (_jsx(MainEditorStoreContext.Provider, { value: {
            state: storeRef.current,
        }, children: children }));
};
//# sourceMappingURL=MainEditorStoreProvider.js.map
