{-# LANGUAGE TemplateHaskell #-}

-- | Effect for working with an interaction
module Calamity.Interactions.Eff (
  InteractionEff (..),
  getInteraction,
  getInteractionID,
  getApplicationID,
  getInteractionToken,
  getInteractionUser,
) where

import Calamity.Types.Model.Interaction
import Calamity.Types.Model.User (User)
import Calamity.Types.Snowflake
import Control.Applicative ((<|>))
import Control.Lens ((^.), (^?), _Just)
import Data.Maybe (fromJust)
import Polysemy
import qualified Polysemy as P

data InteractionEff m a where
  GetInteraction :: InteractionEff m Interaction

makeSem ''InteractionEff

getInteractionID :: P.Member InteractionEff r => P.Sem r (Snowflake Interaction)
getInteractionID :: Sem r (Snowflake Interaction)
getInteractionID = (Interaction
-> Getting
     (Snowflake Interaction) Interaction (Snowflake Interaction)
-> Snowflake Interaction
forall s a. s -> Getting a s a -> a
^. IsLabel
  "id"
  (Getting
     (Snowflake Interaction) Interaction (Snowflake Interaction))
Getting (Snowflake Interaction) Interaction (Snowflake Interaction)
#id) (Interaction -> Snowflake Interaction)
-> Sem r Interaction -> Sem r (Snowflake Interaction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r Interaction
forall (r :: EffectRow).
Member InteractionEff r =>
Sem r Interaction
getInteraction

getApplicationID :: P.Member InteractionEff r => P.Sem r (Snowflake Application)
getApplicationID :: Sem r (Snowflake Application)
getApplicationID = (Interaction
-> Getting
     (Snowflake Application) Interaction (Snowflake Application)
-> Snowflake Application
forall s a. s -> Getting a s a -> a
^. IsLabel
  "applicationID"
  (Getting
     (Snowflake Application) Interaction (Snowflake Application))
Getting (Snowflake Application) Interaction (Snowflake Application)
#applicationID) (Interaction -> Snowflake Application)
-> Sem r Interaction -> Sem r (Snowflake Application)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r Interaction
forall (r :: EffectRow).
Member InteractionEff r =>
Sem r Interaction
getInteraction

getInteractionToken :: P.Member InteractionEff r => P.Sem r InteractionToken
getInteractionToken :: Sem r InteractionToken
getInteractionToken = (Interaction
-> Getting InteractionToken Interaction InteractionToken
-> InteractionToken
forall s a. s -> Getting a s a -> a
^. IsLabel
  "token" (Getting InteractionToken Interaction InteractionToken)
Getting InteractionToken Interaction InteractionToken
#token) (Interaction -> InteractionToken)
-> Sem r Interaction -> Sem r InteractionToken
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r Interaction
forall (r :: EffectRow).
Member InteractionEff r =>
Sem r Interaction
getInteraction

getInteractionUser :: P.Member InteractionEff r => P.Sem r (Snowflake User)
getInteractionUser :: Sem r (Snowflake User)
getInteractionUser = do
  Interaction
int <- Sem r Interaction
forall (r :: EffectRow).
Member InteractionEff r =>
Sem r Interaction
getInteraction
  let uid :: Maybe (Snowflake User)
uid = Interaction
int Interaction
-> Getting (First (Snowflake User)) Interaction (Snowflake User)
-> Maybe (Snowflake User)
forall s a. s -> Getting (First a) s a -> Maybe a
^? IsLabel
  "user"
  ((Maybe User -> Const (First (Snowflake User)) (Maybe User))
   -> Interaction -> Const (First (Snowflake User)) Interaction)
(Maybe User -> Const (First (Snowflake User)) (Maybe User))
-> Interaction -> Const (First (Snowflake User)) Interaction
#user ((Maybe User -> Const (First (Snowflake User)) (Maybe User))
 -> Interaction -> Const (First (Snowflake User)) Interaction)
-> ((Snowflake User
     -> Const (First (Snowflake User)) (Snowflake User))
    -> Maybe User -> Const (First (Snowflake User)) (Maybe User))
-> Getting (First (Snowflake User)) Interaction (Snowflake User)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (User -> Const (First (Snowflake User)) User)
-> Maybe User -> Const (First (Snowflake User)) (Maybe User)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just ((User -> Const (First (Snowflake User)) User)
 -> Maybe User -> Const (First (Snowflake User)) (Maybe User))
-> ((Snowflake User
     -> Const (First (Snowflake User)) (Snowflake User))
    -> User -> Const (First (Snowflake User)) User)
-> (Snowflake User
    -> Const (First (Snowflake User)) (Snowflake User))
-> Maybe User
-> Const (First (Snowflake User)) (Maybe User)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "id"
  ((Snowflake User
    -> Const (First (Snowflake User)) (Snowflake User))
   -> User -> Const (First (Snowflake User)) User)
(Snowflake User -> Const (First (Snowflake User)) (Snowflake User))
-> User -> Const (First (Snowflake User)) User
#id
      mid :: Maybe (Snowflake User)
mid = Interaction
int Interaction
-> Getting (First (Snowflake User)) Interaction (Snowflake User)
-> Maybe (Snowflake User)
forall s a. s -> Getting (First a) s a -> Maybe a
^? IsLabel
  "member"
  ((Maybe Member -> Const (First (Snowflake User)) (Maybe Member))
   -> Interaction -> Const (First (Snowflake User)) Interaction)
(Maybe Member -> Const (First (Snowflake User)) (Maybe Member))
-> Interaction -> Const (First (Snowflake User)) Interaction
#member ((Maybe Member -> Const (First (Snowflake User)) (Maybe Member))
 -> Interaction -> Const (First (Snowflake User)) Interaction)
-> ((Snowflake User
     -> Const (First (Snowflake User)) (Snowflake User))
    -> Maybe Member -> Const (First (Snowflake User)) (Maybe Member))
-> Getting (First (Snowflake User)) Interaction (Snowflake User)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Member -> Const (First (Snowflake User)) Member)
-> Maybe Member -> Const (First (Snowflake User)) (Maybe Member)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just ((Member -> Const (First (Snowflake User)) Member)
 -> Maybe Member -> Const (First (Snowflake User)) (Maybe Member))
-> ((Snowflake User
     -> Const (First (Snowflake User)) (Snowflake User))
    -> Member -> Const (First (Snowflake User)) Member)
-> (Snowflake User
    -> Const (First (Snowflake User)) (Snowflake User))
-> Maybe Member
-> Const (First (Snowflake User)) (Maybe Member)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IsLabel
  "id"
  ((Snowflake User
    -> Const (First (Snowflake User)) (Snowflake User))
   -> Member -> Const (First (Snowflake User)) Member)
(Snowflake User -> Const (First (Snowflake User)) (Snowflake User))
-> Member -> Const (First (Snowflake User)) Member
#id
  Snowflake User -> Sem r (Snowflake User)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Snowflake User -> Sem r (Snowflake User))
-> (Maybe (Snowflake User) -> Snowflake User)
-> Maybe (Snowflake User)
-> Sem r (Snowflake User)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Snowflake User) -> Snowflake User
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe (Snowflake User) -> Sem r (Snowflake User))
-> Maybe (Snowflake User) -> Sem r (Snowflake User)
forall a b. (a -> b) -> a -> b
$ Maybe (Snowflake User)
uid Maybe (Snowflake User)
-> Maybe (Snowflake User) -> Maybe (Snowflake User)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (Snowflake User)
mid