const loadingPagination = (machines, pagination) => {
  pagination.paginationOptions.total = machines.length
  pagination.paginationOptions.total_pages = Math.ceil(pagination.paginationOptions.total / pagination.paginationOptions.page_size)
  return {machines: machines.slice(0, pagination.paginationOptions.page_size), pagination}
}

const setPagination = (machines, pagination, updatedData) => {
  const pageNumber = Number(updatedData.pageNumber)
  const pageSize = Number(updatedData.pageSize)
  pagination.paginationOptions.total_pages = Math.ceil(pagination.paginationOptions.total / pageSize)
  const updatedPaginationOptions = {...pagination.paginationOptions, page_number: pageNumber, page_size: pageSize}
  const updatedPagination = {...pagination, paginationOptions: updatedPaginationOptions}
  const beginPageDataIdx = pageSize === 1 ? 0 : (pageNumber - 1) * pageSize
  const endPageDataIdx = beginPageDataIdx + pageSize
  const isEndPageDataIdxMore = endPageDataIdx > machines.length
  const currentTableData = isEndPageDataIdxMore ? machines.slice(beginPageDataIdx) : machines.slice(beginPageDataIdx, endPageDataIdx)
  return {currentTableData, updatedPagination}
}

export {loadingPagination, setPagination}