import * as React from "react";
import { warnOnce } from "@intelligentgraphics/ig.utilities";
import { Compat } from "@intelligentgraphics/igd.productdata";
export const SessionContext = React.createContext(undefined);
SessionContext.displayName = "SessionContext";
const createSessionContext = (contextValue) => {
    if (contextValue === undefined) {
        return {
            session: Compat.session,
        };
    }
    return contextValue;
};
export const createWithSessionContext = () => (component) => (props) => {
    const contextValue = React.useContext(SessionContext);
    const sessionContext = createSessionContext(contextValue);
    if (sessionContext.session === undefined && props.session === undefined) {
        warnOnce(`No session could be found. Please provide a session through either one of these methods:
- add it directly as a prop to the target component
- add a SessionContext.Provider above the component in the react tree
- initialize productdata's Compat module with a session`);
    }
    return React.createElement(component, {
        ...sessionContext,
        ...props,
    });
};
export const withSessionContext = createWithSessionContext();
export const useSessionContext = () => {
    const contextValue = React.useContext(SessionContext);
    const sessionContext = createSessionContext(contextValue);
    if (sessionContext.session === undefined) {
        warnOnce(`No session could be found. Please provide a session through either one of these methods:
- add a SessionContext.Provider above the component in the react tree
- initialize productdata's Compat module with a session`);
    }
    return sessionContext;
};
