module IHP.Pagination.Helpers where

import IHP.Prelude
import IHP.Pagination.Types

-- | Gets the number of the last page from a 'Pagination' state. Uses the total
-- number of items and the page size to calculate the final page.
getLastPage :: Pagination -> Int
getLastPage :: Pagination -> Int
getLastPage Pagination { Int
$sel:pageSize:Pagination :: Pagination -> Int
pageSize :: Int
pageSize, Int
$sel:totalItems:Pagination :: Pagination -> Int
totalItems :: Int
totalItems } =
    if Int
pageSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then
        ((Int
totalItems Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
pageSize) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    else
        Int
totalItems Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
pageSize

-- | Returns 'True' if there is a next page in the pagination, 'False' otherwise.
hasNextPage :: Pagination -> Bool
hasNextPage :: Pagination -> Bool
hasNextPage pagination :: Pagination
pagination@Pagination { Int
$sel:currentPage:Pagination :: Pagination -> Int
currentPage :: Int
currentPage } =
    Pagination -> Int
getLastPage Pagination
pagination Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
currentPage

-- | Returns 'True' if there is a previous page in the pagination, 'False' otherwise.
hasPreviousPage :: Pagination -> Bool
hasPreviousPage :: Pagination -> Bool
hasPreviousPage pagination :: Pagination
pagination@Pagination { Int
currentPage :: Int
$sel:currentPage:Pagination :: Pagination -> Int
currentPage } =
    Int
currentPage Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1