import { useReducer } from '@tarojs/taro';
import Actions from '@/types/Store/Actions';

type Pagination = {
  pageNum: number;
  pageSize: number;
  total: number;
};

const initState: Pagination = {
  pageNum: 1,
  pageSize: 10,
  total: 0,
};
const reducer = (state: Pagination, action: Actions): Pagination => {
  switch (action.type) {
    case 'updatePagination':
      return { ...state, ...action.payload };
    default:
      return state;
  }
};

const usePagination = (
  initData?: Pagination,
): [Pagination, (e: Pagination) => void] => {
  const [state, dispatch] = useReducer(reducer, { ...initState, ...initData });
  const updatePagination = (e: Pagination) => {
    dispatch({ type: 'updatePagination', payload: e });
  };

  return [state, updatePagination];
};

export default usePagination;
