{-# 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 Data.Maybe (fromJust)
import Optics ((%), (^.), (^?), _Just)
import Polysemy
import Polysemy qualified as P

data InteractionEff m a where
  GetInteraction :: InteractionEff m Interaction

makeSem ''InteractionEff

getInteractionID :: P.Member InteractionEff r => P.Sem r (Snowflake Interaction)
getInteractionID :: forall (r :: EffectRow).
Member InteractionEff r =>
Sem r (Snowflake Interaction)
getInteractionID = (Interaction
-> Optic' A_Lens NoIx Interaction (Snowflake Interaction)
-> Snowflake Interaction
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx 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 :: forall (r :: EffectRow).
Member InteractionEff r =>
Sem r (Snowflake Application)
getApplicationID = (Interaction
-> Optic' A_Lens NoIx Interaction (Snowflake Application)
-> Snowflake Application
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx 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 :: forall (r :: EffectRow).
Member InteractionEff r =>
Sem r InteractionToken
getInteractionToken = (Interaction
-> Optic' A_Lens NoIx Interaction InteractionToken
-> InteractionToken
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx 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 :: forall (r :: EffectRow).
Member InteractionEff r =>
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
-> Optic' An_AffineTraversal NoIx Interaction (Snowflake User)
-> Maybe (Snowflake User)
forall k s (is :: IxList) a.
Is k An_AffineFold =>
s -> Optic' k is s a -> Maybe a
^? Optic A_Lens NoIx Interaction Interaction (Maybe User) (Maybe User)
#user Optic A_Lens NoIx Interaction Interaction (Maybe User) (Maybe User)
-> Optic A_Prism NoIx (Maybe User) (Maybe User) User User
-> Optic An_AffineTraversal NoIx Interaction Interaction User User
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic A_Prism NoIx (Maybe User) (Maybe User) User User
forall a b. Prism (Maybe a) (Maybe b) a b
_Just Optic An_AffineTraversal NoIx Interaction Interaction User User
-> Optic A_Lens NoIx User User (Snowflake User) (Snowflake User)
-> Optic' An_AffineTraversal NoIx Interaction (Snowflake User)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic A_Lens NoIx User User (Snowflake User) (Snowflake User)
#id
      mid :: Maybe (Snowflake User)
mid = Interaction
int Interaction
-> Optic' An_AffineTraversal NoIx Interaction (Snowflake User)
-> Maybe (Snowflake User)
forall k s (is :: IxList) a.
Is k An_AffineFold =>
s -> Optic' k is s a -> Maybe a
^? Optic
  A_Lens NoIx Interaction Interaction (Maybe Member) (Maybe Member)
#member Optic
  A_Lens NoIx Interaction Interaction (Maybe Member) (Maybe Member)
-> Optic A_Prism NoIx (Maybe Member) (Maybe Member) Member Member
-> Optic
     An_AffineTraversal NoIx Interaction Interaction Member Member
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic A_Prism NoIx (Maybe Member) (Maybe Member) Member Member
forall a b. Prism (Maybe a) (Maybe b) a b
_Just Optic An_AffineTraversal NoIx Interaction Interaction Member Member
-> Optic
     A_Lens NoIx Member Member (Snowflake User) (Snowflake User)
-> Optic' An_AffineTraversal NoIx Interaction (Snowflake User)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic A_Lens NoIx Member Member (Snowflake User) (Snowflake User)
#id
  Snowflake User -> Sem r (Snowflake User)
forall a. a -> Sem r a
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 a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (Snowflake User)
mid