"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PCEditorStoreProvider = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const store_1 = require("../../common/store.js");
const PCEditorStoreContext_1 = require("./PCEditorStoreContext.js");
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 = (0, store_1.createPCEditorStore)();
    }
    return ((0, jsx_runtime_1.jsx)(PCEditorStoreContext_1.PCEditorStoreContext.Provider, { value: {
            state: storeRef.current,
        }, children: children }));
};
exports.PCEditorStoreProvider = PCEditorStoreProvider;
//# sourceMappingURL=PCEditorStoreProvider.js.map
