import type { ReactElement, ReactNode } from '../core' import { useContext } from './hooks' export const REACT_CONTEXT_TYPE = Symbol.for('react.context') export const REACT_PROVIDER_TYPE = Symbol.for('react.provider') export const REACT_CONSUMER_TYPE = Symbol.for('react.consumer') export interface Context { $$typeof: typeof REACT_CONTEXT_TYPE _currentValue: T Provider: ProviderExoticComponent<{ value: T; children?: ReactNode }> Consumer: ConsumerExoticComponent displayName?: string } export interface ProviderExoticComponent

{ $$typeof: typeof REACT_PROVIDER_TYPE _context: Context (props: P): ReactElement } export interface ConsumerExoticComponent { $$typeof: typeof REACT_CONSUMER_TYPE _context: Context (props: { children: (value: T) => ReactNode }): ReactElement } export function createContext(defaultValue: T): Context { const context: Context = { $$typeof: REACT_CONTEXT_TYPE, _currentValue: defaultValue, } as Context const Provider: any = function Provider(_props: any): any { if (process.env.NODE_ENV !== 'production') { throw new Error('Provider components are handled by the renderer.') } throw new Error() } Provider.$$typeof = REACT_PROVIDER_TYPE Provider._context = context const Consumer: any = function Consumer(props: { children: (v: T) => any }): any { return props.children(useContext(context)) } Consumer.$$typeof = REACT_CONSUMER_TYPE Consumer._context = context context.Provider = Provider context.Consumer = Consumer return context }