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

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

export const settingsPageContext = context;
export const SettingsPageContextProvider = context.Provider;
export const SettingsPageContextConsumer = context.Consumer;

export const useSettingsPageContext = <
  T,
  V extends FieldValues = FieldValues,
>() => {
  const value = useContext(settingsPageContext);
  assertContextValue('SettingsPageProvider', value);
  return value as SettingsPageState<T, V>;
};
