import { createContext, useContext } from "react";

export interface TableContextProps {
  count: number;
  page: number;
  rowsPerPage: number;
  rowsPerPageOptions: number[];
  changePage: (event: React.MouseEvent<HTMLButtonElement> | null, newPage: number) => void;
  changeRowsPerPage: (event: React.ChangeEvent<HTMLInputElement>) => void;
}

export const TableContext = createContext<TableContextProps>(
  undefined as unknown as TableContextProps,
);

export const useTableContext = () => {
  const context = useContext(TableContext);
  if (context == null) {
    throw new Error(
      "useTableContext must be used within a TableProvider. You are most likely not using the correct <Table> parent component.",
    );
  }

  return context;
};
