export type SortDir = 'ASC' | 'DESC'

export interface Sort {
  attribute: string
  dir: SortDir
}

export interface PageRequest {
  page: number
  size: number
  sorts: Sort[]
}

export function toQueryParams(pageRequest: PageRequest): string {
  const params = new URLSearchParams()
  params.append('page', pageRequest.page + '')
  params.append('size', pageRequest.size + '')
  for (const sort of pageRequest.sorts) {
    params.append('sort', sort.attribute + ',' + sort.dir)
  }
  return params.toString()
}

export interface Page<T> {
  items: T[]
  pageRequest: PageRequest
  totalElements: number
  totalPages: number
  isFirst: boolean
  isLast: boolean
}

export function defaultPage<T>(): Page<T> {
  return {
    items: [],
    pageRequest: { page: 1, size: 5, sorts: [] },
    totalElements: 0,
    totalPages: 0,
    isFirst: true,
    isLast: true,
  }
}
