import { createContext, useContext } from 'react';
import { TableState } from '../state';
import { FiltersMap } from '@wix/bex-core';
import { assertContextValue } from '../createContext';

export const TableContext = createContext<TableState<any, any> | null>(null);

export const TableProvider = TableContext.Provider;
export const TableConsumer = TableContext.Consumer;

export const useTableContext = <T, F extends FiltersMap>() => {
  const value = useContext(TableContext);
  assertContextValue('TableProvider', value);
  return value as TableState<T, F>;
};
