{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Server.Deriving.Channels
  ( channelResolver,
    ChannelsConstraint,
  )
where

import Control.Monad.Except (throwError)
import qualified Data.HashMap.Lazy as HM
import Data.Morpheus.App.Internal.Resolving
  ( Channel,
    Resolver,
    ResolverState,
    SubscriptionField (..),
  )
import Data.Morpheus.Internal.Utils
  ( selectBy,
  )
import Data.Morpheus.Server.Deriving.Decode
  ( DecodeConstraint,
    decodeArguments,
  )
import Data.Morpheus.Server.Deriving.Utils
  ( ConsRep (..),
    DataType (..),
    FieldRep (..),
    TypeConstraint (..),
    TypeRep (..),
    toValue,
  )
import Data.Morpheus.Server.Types.GQLType (GQLType)
import Data.Morpheus.Server.Types.Types (Undefined)
import Data.Morpheus.Types.Internal.AST
  ( FieldName,
    OUT,
    SUBSCRIPTION,
    Selection (..),
    SelectionContent (..),
    VALID,
    internal,
  )
import GHC.Generics
import Relude hiding (Undefined)

newtype DerivedChannel e = DerivedChannel
  { forall e. DerivedChannel e -> Channel e
_unpackChannel :: Channel e
  }

type ChannelRes (e :: Type) = Selection VALID -> ResolverState (DerivedChannel e)

type ChannelsConstraint e m (subs :: (Type -> Type) -> Type) =
  ExploreChannels (IsUndefined (subs (Resolver SUBSCRIPTION e m))) e (subs (Resolver SUBSCRIPTION e m))

channelResolver ::
  forall e m subs.
  ChannelsConstraint e m subs =>
  subs (Resolver SUBSCRIPTION e m) ->
  Selection VALID ->
  ResolverState (Channel e)
channelResolver :: forall e (m :: * -> *) (subs :: (* -> *) -> *).
ChannelsConstraint e m subs =>
subs (Resolver SUBSCRIPTION e m)
-> Selection VALID -> ResolverState (Channel e)
channelResolver subs (Resolver SUBSCRIPTION e m)
value = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall e. DerivedChannel e -> Channel e
_unpackChannel forall b c a. (b -> c) -> (a -> b) -> a -> c
. Selection VALID -> ResolverState (DerivedChannel e)
channelSelector
  where
    channelSelector ::
      Selection VALID ->
      ResolverState (DerivedChannel e)
    channelSelector :: Selection VALID -> ResolverState (DerivedChannel e)
channelSelector =
      forall e. HashMap FieldName (ChannelRes e) -> ChannelRes e
selectBySelection
        ( forall (t :: Bool) e a (f :: Bool -> *).
ExploreChannels t e a =>
f t -> a -> HashMap FieldName (ChannelRes e)
exploreChannels
            (forall {k} (t :: k). Proxy t
Proxy @(IsUndefined (subs (Resolver SUBSCRIPTION e m))))
            subs (Resolver SUBSCRIPTION e m)
value
        )

selectBySelection ::
  HashMap FieldName (ChannelRes e) ->
  Selection VALID ->
  ResolverState (DerivedChannel e)
selectBySelection :: forall e. HashMap FieldName (ChannelRes e) -> ChannelRes e
selectBySelection HashMap FieldName (ChannelRes e)
channels = Selection VALID -> ResolverState (Selection VALID)
withSubscriptionSelection forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall e. HashMap FieldName (ChannelRes e) -> ChannelRes e
selectSubscription HashMap FieldName (ChannelRes e)
channels

selectSubscription ::
  HashMap FieldName (ChannelRes e) ->
  Selection VALID ->
  ResolverState (DerivedChannel e)
selectSubscription :: forall e. HashMap FieldName (ChannelRes e) -> ChannelRes e
selectSubscription HashMap FieldName (ChannelRes e)
channels sel :: Selection VALID
sel@Selection {FieldName
selectionName :: forall (s :: Stage). Selection s -> FieldName
selectionName :: FieldName
selectionName} =
  forall e (m :: * -> *) k (c :: * -> *) a.
(MonadError e m, IsMap k c, Monad m) =>
e -> k -> c a -> m a
selectBy
    (GQLError -> GQLError
internal GQLError
"invalid subscription: no channel is selected.")
    FieldName
selectionName
    HashMap FieldName (ChannelRes e)
channels
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Selection VALID
sel forall a b. a -> (a -> b) -> b
&)

withSubscriptionSelection :: Selection VALID -> ResolverState (Selection VALID)
withSubscriptionSelection :: Selection VALID -> ResolverState (Selection VALID)
withSubscriptionSelection Selection {selectionContent :: forall (s :: Stage). Selection s -> SelectionContent s
selectionContent = SelectionSet SelectionSet VALID
selSet} =
  case forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SelectionSet VALID
selSet of
    [Selection VALID
sel] -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Selection VALID
sel
    [Selection VALID]
_ -> forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (GQLError -> GQLError
internal GQLError
"invalid subscription: there can be only one top level selection")
withSubscriptionSelection Selection VALID
_ = forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (GQLError -> GQLError
internal GQLError
"invalid subscription: expected selectionSet")

class GetChannel e a | a -> e where
  getChannel :: a -> ChannelRes e

instance GetChannel e (SubscriptionField (Resolver SUBSCRIPTION e m a)) where
  getChannel :: SubscriptionField (Resolver SUBSCRIPTION e m a) -> ChannelRes e
getChannel SubscriptionField (Resolver SUBSCRIPTION e m a)
x = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall e. Channel e -> DerivedChannel e
DerivedChannel forall a b. (a -> b) -> a -> b
$ forall a.
SubscriptionField a
-> forall e (m :: * -> *) v.
   (a ~ Resolver SUBSCRIPTION e m v) =>
   Channel e
channel SubscriptionField (Resolver SUBSCRIPTION e m a)
x

instance
  DecodeConstraint arg =>
  GetChannel e (arg -> SubscriptionField (Resolver SUBSCRIPTION e m a))
  where
  getChannel :: (arg -> SubscriptionField (Resolver SUBSCRIPTION e m a))
-> ChannelRes e
getChannel arg -> SubscriptionField (Resolver SUBSCRIPTION e m a)
f sel :: Selection VALID
sel@Selection {Arguments VALID
selectionArguments :: forall (s :: Stage). Selection s -> Arguments s
selectionArguments :: Arguments VALID
selectionArguments} =
    forall a. DecodeConstraint a => Arguments VALID -> ResolverState a
decodeArguments Arguments VALID
selectionArguments
      forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall e a. GetChannel e a => a -> ChannelRes e
`getChannel` Selection VALID
sel)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. arg -> SubscriptionField (Resolver SUBSCRIPTION e m a)
f

------------------------------------------------------

type family IsUndefined a :: Bool where
  IsUndefined (Undefined m) = 'True
  IsUndefined a = 'False

class ExploreChannels (t :: Bool) e a where
  exploreChannels :: f t -> a -> HashMap FieldName (ChannelRes e)

instance (GQLType a, Generic a, TypeRep (GetChannel e) (ChannelRes e) (Rep a)) => ExploreChannels 'False e a where
  exploreChannels :: forall (f :: Bool -> *).
f 'False -> a -> HashMap FieldName (ChannelRes e)
exploreChannels f 'False
_ =
    forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
convertNode
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (proxy :: TypeCategory -> *) (kind :: TypeCategory)
       (constraint :: * -> Constraint) value a.
(GQLType a, CategoryValue kind, Generic a,
 TypeRep constraint value (Rep a)) =>
TypeConstraint constraint value Identity
-> proxy kind -> a -> DataType value
toValue
        ( forall (c :: * -> Constraint) v (f :: * -> *).
(forall a. c a => f a -> v) -> TypeConstraint c v f
TypeConstraint (forall e a. GetChannel e a => a -> ChannelRes e
getChannel forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity) :: TypeConstraint (GetChannel e) (ChannelRes e) Identity
        )
        (forall {k} (t :: k). Proxy t
Proxy @OUT)

instance ExploreChannels 'True e (Undefined m) where
  exploreChannels :: forall (f :: Bool -> *).
f 'True -> Undefined m -> HashMap FieldName (ChannelRes e)
exploreChannels f 'True
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall k v. HashMap k v
HM.empty

convertNode :: DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
convertNode :: forall e. DataType (ChannelRes e) -> [(FieldName, ChannelRes e)]
convertNode DataType {tyCons :: forall v. DataType v -> ConsRep v
tyCons = ConsRep {[FieldRep (ChannelRes e)]
consFields :: forall v. ConsRep v -> [FieldRep v]
consFields :: [FieldRep (ChannelRes e)]
consFields}} = forall a b. (a -> b) -> [a] -> [b]
map forall {b}. FieldRep b -> (FieldName, b)
toChannels [FieldRep (ChannelRes e)]
consFields
  where
    toChannels :: FieldRep b -> (FieldName, b)
toChannels FieldRep {FieldName
fieldSelector :: forall a. FieldRep a -> FieldName
fieldSelector :: FieldName
fieldSelector, b
fieldValue :: forall a. FieldRep a -> a
fieldValue :: b
fieldValue} = (FieldName
fieldSelector, b
fieldValue)