import { FilterPaginationResult } from '../types'
import { normalizePageSize } from './normalize'

const filterPagination = <T>(
  items: T[] | null | undefined,
  pageSize: number,
  currentPage: number
): FilterPaginationResult<T> => {
  if (!items?.length) {
    return { contentItems: [] }
  }

  const safePageSize = normalizePageSize(pageSize)
  const startIndex = (currentPage - 1) * safePageSize

  return {
    contentItems: items.slice(startIndex, startIndex + safePageSize),
  }
}

export default filterPagination
