{-# LANGUAGE TemplateHaskell #-}

-- | Command prefix parsing effect
module Calamity.Commands.ParsePrefix
    ( ParsePrefix(..)
    , parsePrefix
    , useConstantPrefix ) where

import           Calamity.Types.Model.Channel.Message

import           Control.Lens

import qualified Data.Text.Lazy                       as L

import qualified Polysemy                             as P

-- | An effect for parsing the prefix of a command.
data ParsePrefix m a where
  ParsePrefix :: Message -> ParsePrefix m (Maybe (L.Text, L.Text))

P.makeSem ''ParsePrefix

-- | A default interpretation for 'ParsePrefix' that uses a single constant prefix.
useConstantPrefix :: L.Text -> P.Sem (ParsePrefix ': r) a -> P.Sem r a
useConstantPrefix :: Text -> Sem (ParsePrefix : r) a -> Sem r a
useConstantPrefix pre :: Text
pre = (forall x (m :: * -> *). ParsePrefix m x -> Sem r x)
-> Sem (ParsePrefix : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall x (m :: * -> *). e m x -> Sem r x)
-> Sem (e : r) a -> Sem r a
P.interpret (\case
                                       ParsePrefix m -> Maybe (Text, Text) -> Sem r (Maybe (Text, Text))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text
pre, ) (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> Maybe Text
L.stripPrefix Text
pre (Message
m Message -> Getting Text Message Text -> Text
forall s a. s -> Getting a s a -> a
^. IsLabel "content" (Getting Text Message Text)
Getting Text Message Text
#content)))