{- HLINT ignore "Use let" -}

module Effectful.Zoo.Amazonka.Api.Run
  ( runDataLogAwsLogEntryToLog,
    runDataLogAwsLogEntryToLogWith,
    runDataLogAwsLogEntryLocalToLogWith,
  ) where

import Control.Monad.IO.Class
import Data.ByteString.Builder qualified as B
import Data.Text.Lazy qualified as LT
import Data.Text.Lazy.Encoding qualified as LT
import Data.Time.Clock qualified as IO
import Effectful
import Effectful.Zoo.Amazonka.Api
import Effectful.Zoo.Amazonka.Data
import Effectful.Zoo.Core
import Effectful.Zoo.DataLog.Api
import Effectful.Zoo.DataLog.Data.LogEntry
import Effectful.Zoo.DataLog.Dynamic
import Effectful.Zoo.Log.Data.LogMessage
import Effectful.Zoo.Log.Data.Severity
import HaskellWorks.Prelude

runDataLogAwsLogEntryToLog :: forall a r. ()
  => r <: DataLog (LogEntry (LogMessage Text))
  => r <: IOE
  => Eff (DataLog AwsLogEntry : r) a
  -> Eff r a
runDataLogAwsLogEntryToLog :: forall a (r :: [Effect]).
(r <: DataLog (LogEntry (LogMessage Text)), r <: IOE) =>
Eff (DataLog AwsLogEntry : r) a -> Eff r a
runDataLogAwsLogEntryToLog =
  (AwsLogLevel -> Severity)
-> Eff (DataLog AwsLogEntry : r) a -> Eff r a
forall a (r :: [Effect]).
(r <: DataLog (LogEntry (LogMessage Text)), r <: IOE) =>
(AwsLogLevel -> Severity)
-> Eff (DataLog AwsLogEntry : r) a -> Eff r a
runDataLogAwsLogEntryToLogWith AwsLogLevel -> Severity
awsLogLevelToSeverity

runDataLogAwsLogEntryToLogWith :: forall a r. ()
  => r <: DataLog (LogEntry (LogMessage Text))
  => r <: IOE
  => (AwsLogLevel -> Severity)
  -> Eff (DataLog AwsLogEntry : r) a
  -> Eff r a
runDataLogAwsLogEntryToLogWith :: forall a (r :: [Effect]).
(r <: DataLog (LogEntry (LogMessage Text)), r <: IOE) =>
(AwsLogLevel -> Severity)
-> Eff (DataLog AwsLogEntry : r) a -> Eff r a
runDataLogAwsLogEntryToLogWith AwsLogLevel -> Severity
mapSeverity
  = (AwsLogLevel -> Severity)
-> (AwsLogEntry -> AwsLogEntry)
-> Eff (DataLog AwsLogEntry : r) a
-> Eff r a
forall a (r :: [Effect]).
(r <: DataLog (LogEntry (LogMessage Text)), r <: IOE) =>
(AwsLogLevel -> Severity)
-> (AwsLogEntry -> AwsLogEntry)
-> Eff (DataLog AwsLogEntry : r) a
-> Eff r a
runDataLogAwsLogEntryLocalToLogWith AwsLogLevel -> Severity
mapSeverity AwsLogEntry -> AwsLogEntry
forall a. a -> a
id

runDataLogAwsLogEntryLocalToLogWith :: forall a r. ()
  => r <: DataLog (LogEntry (LogMessage Text))
  => r <: IOE
  => (AwsLogLevel -> Severity)
  -> (AwsLogEntry -> AwsLogEntry)
  -> Eff (DataLog AwsLogEntry : r) a
  -> Eff r a
runDataLogAwsLogEntryLocalToLogWith :: forall a (r :: [Effect]).
(r <: DataLog (LogEntry (LogMessage Text)), r <: IOE) =>
(AwsLogLevel -> Severity)
-> (AwsLogEntry -> AwsLogEntry)
-> Eff (DataLog AwsLogEntry : r) a
-> Eff r a
runDataLogAwsLogEntryLocalToLogWith AwsLogLevel -> Severity
mapSeverity AwsLogEntry -> AwsLogEntry
context =
  (HasCallStack => AwsLogEntry -> Eff r ())
-> Eff (DataLog AwsLogEntry : r) a -> Eff r a
forall i (r :: [Effect]) a.
HasCallStack =>
(HasCallStack => i -> Eff r ()) -> Eff (DataLog i : r) a -> Eff r a
runDataLog ((HasCallStack => AwsLogEntry -> Eff r ())
 -> Eff (DataLog AwsLogEntry : r) a -> Eff r a)
-> (HasCallStack => AwsLogEntry -> Eff r ())
-> Eff (DataLog AwsLogEntry : r) a
-> Eff r a
forall a b. (a -> b) -> a -> b
$ \AwsLogEntry
logEntry -> do
    UTCTime
now <- IO UTCTime -> Eff r UTCTime
forall a. IO a -> Eff r a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
IO.getCurrentTime

    let entry :: AwsLogEntry
entry = AwsLogEntry -> AwsLogEntry
context AwsLogEntry
logEntry
    let message :: LogMessage Text
message =
          Severity -> Text -> LogMessage Text
forall i. Severity -> i -> LogMessage i
LogMessage (AwsLogLevel -> Severity
mapSeverity AwsLogEntry
entry.logLevel) (Text -> LogMessage Text) -> Text -> LogMessage Text
forall a b. (a -> b) -> a -> b
$
            Text -> Text
LT.toStrict (ByteString -> Text
LT.decodeUtf8 (Builder -> ByteString
B.toLazyByteString AwsLogEntry
entry.builder))

    LogEntry (LogMessage Text) -> Eff r ()
forall (r :: [Effect]) i.
(HasCallStack, r <: DataLog i) =>
i -> Eff r ()
dataLog (LogEntry (LogMessage Text) -> Eff r ())
-> LogEntry (LogMessage Text) -> Eff r ()
forall a b. (a -> b) -> a -> b
$
      LogEntry
        { $sel:message:LogEntry :: LogMessage Text
message = LogMessage Text
message
        , $sel:time:LogEntry :: UTCTime
time = UTCTime
now
        , $sel:source:LogEntry :: CallStack
source = AwsLogEntry
entry.callStack
        }