import React, { forwardRef } from 'react';
import type { TablePaginationProps } from '@mui/material';
import { TablePagination as MuiTablePagination } from '@mui/material';

import { omitUndefinedProps } from '@/mui/helpers';

const TablePagination = forwardRef<HTMLElement, TablePaginationProps>(
  ({ onPageChange, rowsPerPage, ...props }, ref) => {
    const count = Math.max(0, props.count);
    const page = Math.max(1, props.page) - 1;

    return (
      <MuiTablePagination
        {...omitUndefinedProps(props)}
        onPageChange={onPageChange}
        rowsPerPage={rowsPerPage}
        count={count}
        color="neutral"
        page={page}
        ref={ref}
      />
    );
  },
);

TablePagination.displayName = 'TablePagination';

export default TablePagination;
