import React__default, { useContext } from 'react';
import { noop } from '../../utils/index.mjs';
import { useEvent } from '../hooks/useEvent.mjs';
import { DEFAULT_MFA_STATE } from './MfaState.mjs';
import { reducer } from './reducer/index.mjs';
import { AppScreens } from './types/AppScreens.mjs';
import { ErrorType } from './types/ErrorType.mjs';
import { AuthFlowType } from '../../../../core/src/public/b2b/ui.mjs';

const DEFAULT_STATE = {
    screen: AppScreens.Main,
    screenHistory: [],
    formState: {
        emailState: {
            userSuppliedEmail: ''
        },
        otpState: {
            codeExpiration: null
        },
        discoveryState: {
            email: '',
            discoveredOrganizations: []
        },
        passwordState: {
            email: ''
        },
        ssoDiscoveryState: {
            connections: []
        }
    },
    flowState: {
        type: AuthFlowType.Organization,
        organization: null
    },
    mfa: DEFAULT_MFA_STATE,
    primary: {},
    error: {
        type: ErrorType.Default,
        canGoBack: false
    }
};
const GlobalContext = /*#__PURE__*/ React__default.createContext({
    client: {},
    config: {},
    state: [
        DEFAULT_STATE,
        ()=>null
    ]
});
const GlobalContextProvider = ({ children, client, config, callbacks, initialState })=>{
    const state = React__default.useReducer(reducer, initialState ?? DEFAULT_STATE);
    return /*#__PURE__*/ React__default.createElement(GlobalContext.Provider, {
        value: {
            client,
            state,
            config,
            callbacks
        }
    }, children);
};
const useGlobalContext = ()=>useContext(GlobalContext);
const useStytch = ()=>useGlobalContext().client;
const useGlobalReducer = ()=>useGlobalContext().state;
const useConfig = ()=>useGlobalContext().config;
const useErrorCallback = ()=>useEvent(useGlobalContext().callbacks?.onError ?? noop);
const useEventCallback = ()=>useEvent(useGlobalContext().callbacks?.onEvent ?? noop);

export { DEFAULT_STATE, GlobalContext, GlobalContextProvider, useConfig, useErrorCallback, useEventCallback, useGlobalContext, useGlobalReducer, useStytch };
//# sourceMappingURL=GlobalContextProvider.mjs.map
