import * as React from "react";
import { withSessionContext, useSessionContext, } from "./SessionContext";
const callbackNames = {
    ItemCreated: "onItemCreated",
    ItemChanged: "onItemChanged",
    ItemConfigured: "onItemConfigured",
    ItemGfxInserted: "onItemGfxInserted",
    ItemGfxRemoved: "onItemGfxRemoved",
    ItemRemoved: "onItemRemoved",
};
export const useNotificationCenterSubscription = (props) => {
    const { session: contextSession } = useSessionContext();
    const session = props.session || contextSession;
    const currentProps = React.useRef(props);
    currentProps.current = props;
    React.useEffect(() => {
        const subscription = session.onAnyEvent(event => {
            const callbackName = callbackNames[event.event];
            if (callbackName !== undefined) {
                const callback = currentProps.current[callbackName];
                if (callback !== undefined) {
                    callback(event.data);
                }
            }
        });
        return () => subscription.destroy();
    }, [session]);
};
const NotificationCenterSubscription = props => {
    useNotificationCenterSubscription(props);
    if (props.children !== undefined) {
        return props.children;
    }
    return null;
};
NotificationCenterSubscription.displayName = "NotificationCenterSubscription";
export default withSessionContext(NotificationCenterSubscription);
