{"version":3,"file":"GlobalContextProvider.mjs","sources":["../../../../../../../../web/src/ui/b2c/GlobalContextProvider.tsx"],"sourcesContent":["import { Callbacks, OTPMethods, StytchProjectConfigurationInput, Wallets } from '@stytch/core/public';\nimport React, { useContext } from 'react';\n\nimport type { StytchClient } from '../../StytchClient';\nimport { StytchLoginConfig } from '../../types';\nimport { noop } from '../../utils';\nimport { useEvent } from '../hooks/useEvent';\nimport { Action, reducer } from './reducer';\n\n/* eslint-disable lingui/no-unlocalized-strings -- internal constants, not user-facing strings */\nexport const AppScreens = {\n  Main: 'Main',\n  FloatingOneTap: 'FloatingOneTap',\n  PasskeyRegistrationStart: 'PasskeyRegistrationStart',\n  PasskeyRegistrationSuccess: 'PasskeyRegistrationSuccess',\n  PasskeyConfirmation: 'PasskeyConfirmation',\n  PasswordCreateOrLogin: 'PasswordCreateOrLogin',\n  PasswordResetForm: 'PasswordResetForm',\n  PasswordForgot: 'PasswordForgot',\n  PasswordBreached: 'PasswordBreached',\n  PasswordSetNew: 'PasswordSetNew',\n  PasswordDedupe: 'PasswordDedupe',\n  PasswordConfirmation: 'PasswordConfirmation',\n  EmailConfirmation: 'EmailConfirmation',\n  OTPAuthenticate: 'OTPAuthenticate',\n  OTPConfirmation: 'OTPConfirmation',\n  CryptoConnect: 'CryptoConnect',\n  CryptoSignMessage: 'CryptoSignMessage',\n  CryptoOtherScreen: 'CryptoOtherScreen',\n  CryptoSetupWallet: 'CryptoSetupWallet',\n  CryptoError: 'CryptoError',\n  CryptoConfirmation: 'CryptoConfirmation',\n} as const;\n/* eslint-enable lingui/no-unlocalized-strings */\n\nexport type AppScreens = keyof typeof AppScreens;\n\nexport type MagicLinkState = {\n  email: string;\n};\n\nexport type OTPState = {\n  type: OTPMethods;\n  methodId: string;\n  otpDestination: string;\n};\n\nexport type PasswordState = {\n  email: string;\n  type: 'new' | 'passwordless' | 'password';\n};\n\nexport type CryptoState = {\n  walletAddress: string;\n  walletOption: Wallets;\n  walletChallenge: string;\n};\n\nexport type ResetPasswordState = {\n  token: string;\n};\n\nexport type FormState = {\n  magicLinkState: MagicLinkState;\n  otpState: OTPState;\n  cryptoState: CryptoState;\n  passwordState: PasswordState;\n  resetPasswordState: ResetPasswordState;\n};\n\nexport type AppState = {\n  screen: AppScreens;\n  formState: FormState;\n};\n\nexport type AppContext<TProjectConfiguration extends StytchProjectConfigurationInput> = {\n  client: StytchClient<TProjectConfiguration>;\n  config: StytchLoginConfig;\n  callbacks?: Callbacks<TProjectConfiguration>;\n  state: [AppState, React.Dispatch<Action>];\n};\n\nexport const DEFAULT_STATE: AppState = {\n  screen: AppScreens.Main,\n  formState: {\n    magicLinkState: {\n      email: '',\n    },\n    otpState: {\n      type: OTPMethods.Email,\n      methodId: '',\n      otpDestination: '',\n    },\n    cryptoState: {\n      walletAddress: '',\n      walletOption: Wallets.Metamask,\n      walletChallenge: '',\n    },\n    passwordState: {\n      email: '',\n      type: 'new',\n    },\n    resetPasswordState: {\n      token: '',\n    },\n  },\n};\n\nexport const GlobalContext = React.createContext<AppContext<StytchProjectConfigurationInput>>({\n  client: {} as StytchClient<StytchProjectConfigurationInput>,\n  config: {} as StytchLoginConfig,\n  state: [DEFAULT_STATE, () => null],\n});\n\nexport type GlobalContextProviderProps<TProjectConfiguration extends StytchProjectConfigurationInput> = {\n  children: React.ReactNode;\n  client: StytchClient<TProjectConfiguration>;\n  config: StytchLoginConfig;\n  callbacks?: Callbacks<TProjectConfiguration>;\n  initialState?: AppState;\n};\n\nexport const GlobalContextProvider = <TProjectConfiguration extends StytchProjectConfigurationInput>({\n  children,\n  client,\n  config,\n  callbacks,\n  initialState,\n}: GlobalContextProviderProps<TProjectConfiguration>) => {\n  const state = React.useReducer(reducer, initialState ?? DEFAULT_STATE);\n\n  return <GlobalContext.Provider value={{ client, state, config, callbacks }}>{children}</GlobalContext.Provider>;\n};\n\nexport const useGlobalContext = () => useContext(GlobalContext);\nexport const useStytch = () => useGlobalContext().client;\nexport const useGlobalReducer = () => useGlobalContext().state;\nexport const useConfig = () => useGlobalContext().config;\nexport const useErrorCallback = () => useEvent(useGlobalContext().callbacks?.onError ?? noop);\nexport const useEventCallback = () => useEvent(useGlobalContext().callbacks?.onEvent ?? noop);\n"],"names":["AppScreens","Main","FloatingOneTap","PasskeyRegistrationStart","PasskeyRegistrationSuccess","PasskeyConfirmation","PasswordCreateOrLogin","PasswordResetForm","PasswordForgot","PasswordBreached","PasswordSetNew","PasswordDedupe","PasswordConfirmation","EmailConfirmation","OTPAuthenticate","OTPConfirmation","CryptoConnect","CryptoSignMessage","CryptoOtherScreen","CryptoSetupWallet","CryptoError","CryptoConfirmation","DEFAULT_STATE","screen","formState","magicLinkState","email","otpState","type","OTPMethods","Email","methodId","otpDestination","cryptoState","walletAddress","walletOption","Wallets","Metamask","walletChallenge","passwordState","resetPasswordState","token","GlobalContext","React","createContext","client","config","state","GlobalContextProvider","children","callbacks","initialState","useReducer","reducer","Provider","value","useGlobalContext","useContext","useStytch","useGlobalReducer","useConfig","useErrorCallback","useEvent","onError","noop","useEventCallback","onEvent"],"mappings":";;;;;;;AASA,kGACO,MAAMA,UAAAA,GAAa;IACxBC,IAAAA,EAAM,MAAA;IACNC,cAAAA,EAAgB,gBAAA;IAChBC,wBAAAA,EAA0B,0BAAA;IAC1BC,0BAAAA,EAA4B,4BAAA;IAC5BC,mBAAAA,EAAqB,qBAAA;IACrBC,qBAAAA,EAAuB,uBAAA;IACvBC,iBAAAA,EAAmB,mBAAA;IACnBC,cAAAA,EAAgB,gBAAA;IAChBC,gBAAAA,EAAkB,kBAAA;IAClBC,cAAAA,EAAgB,gBAAA;IAChBC,cAAAA,EAAgB,gBAAA;IAChBC,oBAAAA,EAAsB,sBAAA;IACtBC,iBAAAA,EAAmB,mBAAA;IACnBC,eAAAA,EAAiB,iBAAA;IACjBC,eAAAA,EAAiB,iBAAA;IACjBC,aAAAA,EAAe,eAAA;IACfC,iBAAAA,EAAmB,mBAAA;IACnBC,iBAAAA,EAAmB,mBAAA;IACnBC,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