import * as React from 'react' ; import { OpenSearchDashboardsReactContext , OpenSearchDashboardsReactContextValue , OpenSearchDashboardsServices , } from './types' ; import { createReactOverlays } from '../overlays' ; import { createNotifications } from '../notifications' ; const { useMemo , useContext , createElement , createContext } = React ; const defaultContextValue = { services : { } , overlays : createReactOverlays ( { } ) , notifications : createNotifications ( { } ) , } ; export const context = createContext < OpenSearchDashboardsReactContextValue < OpenSearchDashboardsServices > > ( defaultContextValue ) ; export const useOpenSearchDashboards = < Extra extends object = { } > ( ) : OpenSearchDashboardsReactContextValue < OpenSearchDashboardsServices & Extra > => useContext ( ( context as unknown ) as React . Context < OpenSearchDashboardsReactContextValue < OpenSearchDashboardsServices & Extra > > ) ; export const withOpenSearchDashboards = < Props extends { opensearchDashboards : OpenSearchDashboardsReactContextValue < any > ; } > ( type ) : React . FC < Omit < Props , 'opensearchDashboards' > > => { const EnhancedType = ( props ) => { const opensearchDashboards = useOpenSearchDashboards ( ) ; return React . createElement ( type , { ... props , opensearchDashboards } as Props ) ; } ; return EnhancedType ; } ; export const UseOpenSearchDashboards = ( { children } ) => < > { children ( useOpenSearchDashboards ( ) ) } < />; export const createOpenSearchDashboardsReactContext = < Services extends OpenSearchDashboardsServices > ( services ) : OpenSearchDashboardsReactContext < Services > => { const value = { services , overlays : createReactOverlays ( services ) , notifications : createNotifications ( services ) , } ; const Provider = ( { services : newServices = { } , children , } ) => { const oldValue = useOpenSearchDashboards ( ) ; const { value : newValue } = useMemo ( ( ) => createOpenSearchDashboardsReactContext ( { ... services , ... oldValue . services , ... newServices , } ) , [ services , oldValue , newServices ] ) ; return createElement ( context . Provider as React . ComponentType < any > , { value : newValue , children , } ) ; } ; return { value , Provider , Consumer : ( context . Consumer as unknown ) as React . Consumer < OpenSearchDashboardsReactContextValue < Services > > , } ; } ; export const { Provider : OpenSearchDashboardsContextProvider , } = createOpenSearchDashboardsReactContext ( { } ) ;