import { StatefulCallClient , StatefulDeviceManager } from '@internal/calling-stateful-client' ; import React , { createContext , useContext , useEffect , useState } from 'react' ; private export type CallClientContextType = { callClient : StatefulCallClient ; deviceManager : StatefulDeviceManager | undefined ; } ; private export const CallClientContext = createContext < CallClientContextType | undefined > ( undefined ) ; /**
 * Arguments to initialize a  CallClientProvider .
 *
 *  public export interface CallClientProviderProps { children : React . ReactNode ; callClient : StatefulCallClient ; } private const CallClientProviderBase = ( props ) : JSX . Element => { const { callClient } = props ; const [ deviceManager , setDeviceManager ] = useState < StatefulDeviceManager | undefined > ( undefined ) ; /**
     * Initialize the DeviceManager inside CallClientState
     */ useEffect ( ( ) => { callClient . getDeviceManager ( ) . then ( ( manager ) => { manager . getCameras ( ) ; manager . getMicrophones ( ) ; manager . getSpeakers ( ) ; setDeviceManager ( manager as StatefulDeviceManager ) ; } ) . catch ( ( error ) => { throw new Error ( error ) ; } ) ; } , [ callClient ] ) ; const initialState = { callClient , deviceManager } ; return < CallClientContext . Provider value = { initialState } > { props . children } < /CallClientContext.Provider>; } ; /**
 * A  React . Context that stores a  StatefulCallClient .
 *
 * Calling components from this package must be wrapped with a  CallClientProvider .
 *
 *  public export const CallClientProvider = ( props ) : JSX . Element => ( < CallClientProviderBase { ... props } / > ) ; /**
 * Hook to obtain  StatefulCallClient from the provider.
 *
 * Useful when implementing a custom component that utilizes the providers
 * exported from this library.
 *
 *  public export const useCallClient = ( ) : StatefulCallClient => { const context = useContext ( CallClientContext ) ; if ( context === undefined ) { throw new Error ( 'CallClient Context is undefined' ) ; } return context . callClient ; } ; /**
 * Hook to obtain  StatefulDeviceManager from the provider.
 *
 * Useful when implementing a custom component that utilizes the providers
 * exported from this library.
 *
 *  public export const useDeviceManager = ( ) : StatefulDeviceManager | undefined => { return useContext ( CallClientContext ) ?. deviceManager ; } ;