import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { createPCEditorStore } from "../../common/store.js";
import { PCEditorStoreContext } from "./PCEditorStoreContext.js";
export const PCEditorStoreProvider = ({ children }) => {
    const storeRef = React.useRef();
    const syncStore = React.useCallback((message) => {
        if (storeRef.current && message.state) {
            storeRef.current.setState(message.state);
        }
    }, []);
    React.useEffect(() => {
        const onMessage = (e) => {
            const message = e.data;
            syncStore(message);
        };
        window.addEventListener('message', onMessage);
        return () => {
            window.removeEventListener('message', onMessage);
        };
    }, [syncStore]);
    if (!storeRef.current) {
        storeRef.current = createPCEditorStore();
    }
    return (_jsx(PCEditorStoreContext.Provider, { value: {
            state: storeRef.current,
        }, children: children }));
};
//# sourceMappingURL=PCEditorStoreProvider.js.map
