import { EntityPageState } from '../state';
import { createContext, useContext } from 'react';
import { assertContextValue } from '../createContext';
import { FieldValues } from 'react-hook-form';

const context = createContext<EntityPageState<any, any> | null>(null);

export const entityPageContext = context;
export const EntityPageContextProvider = context.Provider;
export const EntityPageContextConsumer = context.Consumer;

export const useEntityPageContext = <
  T,
  V extends FieldValues = FieldValues,
>() => {
  const value = useContext(entityPageContext);
  assertContextValue('EntityPageProvider', value);
  return value as EntityPageState<T, V>;
};
