{-# language FlexibleContexts #-}

module Rel8.Internal.Query.Either
  ( keepLeftTable
  , keepRightTable
  , bitraverseEitherTable
  )
where

-- base
import Prelude

-- comonad
import Control.Comonad ( extract )

-- rel8
import Rel8.Internal.Expr ( Expr )
import Rel8.Internal.Expr.Eq ( (==.) )
import Rel8.Internal.Query ( Query )
import Rel8.Internal.Query.Filter ( where_ )
import Rel8.Internal.Query.Maybe ( optional )
import Rel8.Internal.Table.Either
  ( EitherTable( EitherTable )
  , isLeftTable, isRightTable
  )
import Rel8.Internal.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )


-- | Filter 'EitherTable's, keeping only 'leftTable's.
keepLeftTable :: EitherTable Expr a b -> Query a
keepLeftTable :: forall a b. EitherTable Expr a b -> Query a
keepLeftTable e :: EitherTable Expr a b
e@(EitherTable Expr EitherTag
_ Nullify Expr a
a Nullify Expr b
_) = do
  Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ EitherTable Expr a b -> Expr Bool
forall a b. EitherTable Expr a b -> Expr Bool
isLeftTable EitherTable Expr a b
e
  a -> Query a
forall a. a -> Query a
forall (f :: Context) a. Applicative f => a -> f a
pure (Nullify Expr a -> a
forall a. Nullify Expr a -> a
forall (w :: Context) a. Comonad w => w a -> a
extract Nullify Expr a
a)


-- | Filter 'EitherTable's, keeping only 'rightTable's.
keepRightTable :: EitherTable Expr a b -> Query b
keepRightTable :: forall a b. EitherTable Expr a b -> Query b
keepRightTable e :: EitherTable Expr a b
e@(EitherTable Expr EitherTag
_ Nullify Expr a
_ Nullify Expr b
b) = do
  Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ EitherTable Expr a b -> Expr Bool
forall a b. EitherTable Expr a b -> Expr Bool
isRightTable EitherTable Expr a b
e
  b -> Query b
forall a. a -> Query a
forall (f :: Context) a. Applicative f => a -> f a
pure (Nullify Expr b -> b
forall a. Nullify Expr a -> a
forall (w :: Context) a. Comonad w => w a -> a
extract Nullify Expr b
b)


-- | @bitraverseEitherTable f g x@ will pass all @leftTable@s through @f@ and
-- all @rightTable@s through @g@. The results are then lifted back into
-- @leftTable@ and @rightTable@, respectively. This is similar to 'bitraverse'
-- for 'Either'.
--
-- For example,
--
-- >>> :{
-- select do
--   x <- values (map lit [ Left True, Right (42 :: Int32) ])
--   bitraverseEitherTable (\y -> values [y, not_ y]) (\y -> pure (y * 100)) x
-- :}
-- [ Left True
-- , Left False
-- , Right 4200
-- ]
bitraverseEitherTable :: ()
  => (a -> Query c)
  -> (b -> Query d)
  -> EitherTable Expr a b
  -> Query (EitherTable Expr c d)
bitraverseEitherTable :: forall a c b d.
(a -> Query c)
-> (b -> Query d)
-> EitherTable Expr a b
-> Query (EitherTable Expr c d)
bitraverseEitherTable a -> Query c
f b -> Query d
g e :: EitherTable Expr a b
e@(EitherTable Expr EitherTag
tag Nullify Expr a
_ Nullify Expr b
_) = do
  mc@(MaybeTable _ c) <- Query c -> Query (MaybeTable Expr c)
forall a. Query a -> Query (MaybeTable Expr a)
optional (a -> Query c
f (a -> Query c) -> Query a -> Query c
forall (m :: Context) a b. Monad m => (a -> m b) -> m a -> m b
=<< EitherTable Expr a b -> Query a
forall a b. EitherTable Expr a b -> Query a
keepLeftTable EitherTable Expr a b
e)
  md@(MaybeTable _ d) <- optional (g =<< keepRightTable e)
  where_ $ isJustTable mc ==. isLeftTable e
  where_ $ isJustTable md ==. isRightTable e
  pure $ EitherTable tag c d