-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.Sem.Paging
  ( -- * General paging types
    Page,
    PagingState,
    PagingBounds,
    Paging (..),

    -- * Utilities
    withChunks,

    -- * Simple paging
    SimplePaging,
  )
where

import Data.Kind
import Imports

type family Page p a :: (page :: Type) | page -> p a

type family PagingState p a = (ps :: Type)

type family PagingBounds p a :: Type

class Paging p where
  pageItems :: Page p a -> [a]
  pageHasMore :: Page p a -> Bool
  pageState :: Page p a -> PagingState p a

data SimplePaging

type instance Page SimplePaging a = [a]

type instance PagingState SimplePaging a = ()

type instance PagingBounds SimplePaging a = Int32

instance Paging SimplePaging where
  pageItems :: forall a. Page SimplePaging a -> [a]
pageItems = [a] -> [a]
Page SimplePaging a -> [a]
forall a. a -> a
id
  pageHasMore :: forall a. Page SimplePaging a -> Bool
pageHasMore Page SimplePaging a
_ = Bool
False
  pageState :: forall a. Page SimplePaging a -> PagingState SimplePaging a
pageState Page SimplePaging a
_ = ()

withChunks ::
  (Paging p, Monad m) =>
  (Maybe (PagingState p i) -> m (Page p i)) ->
  ([i] -> m ()) ->
  m ()
withChunks :: forall p (m :: * -> *) i.
(Paging p, Monad m) =>
(Maybe (PagingState p i) -> m (Page p i)) -> ([i] -> m ()) -> m ()
withChunks Maybe (PagingState p i) -> m (Page p i)
pager [i] -> m ()
action = do
  Page p i
page <- Maybe (PagingState p i) -> m (Page p i)
pager Maybe (PagingState p i)
forall a. Maybe a
Nothing
  Page p i -> m ()
go Page p i
page
  where
    go :: Page p i -> m ()
go Page p i
page = do
      [i] -> m ()
action (Page p i -> [i]
forall p a. Paging p => Page p a -> [a]
forall a. Page p a -> [a]
pageItems Page p i
page)
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Page p i -> Bool
forall p a. Paging p => Page p a -> Bool
forall a. Page p a -> Bool
pageHasMore Page p i
page) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
        Page p i
page' <- Maybe (PagingState p i) -> m (Page p i)
pager (PagingState p i -> Maybe (PagingState p i)
forall a. a -> Maybe a
Just (Page p i -> PagingState p i
forall p a. Paging p => Page p a -> PagingState p a
forall a. Page p a -> PagingState p a
pageState Page p i
page))
        Page p i -> m ()
go Page p i
page'