module Plutarch.Trace (
  -- * Info level
  ptraceInfo,
  ptraceInfoShowId,
  ptraceInfoError,
  ptraceInfoIfTrue,
  ptraceInfoIfFalse,

  -- * Debug level
  ptraceDebug,
  ptraceDebugShowId,
  ptraceDebugIfTrue,
  ptraceDebugIfFalse,
  ptraceDebugError,

  -- * Deprecated
  ptrace,
  ptraceShowId,
  ptraceError,
  ptraceIfTrue,
  ptraceIfFalse,
) where

import Data.Kind (Type)
import Plutarch.Builtin.Bool (PBool, pif)
import Plutarch.Internal.Term (
  Config (NoTracing, Tracing),
  LogLevel (LogDebug, LogInfo),
  S,
  Term,
  perror,
  pgetConfig,
  plet,
  (#),
 )
import Plutarch.Internal.Trace (ptrace, ptrace', ptraceDebug, ptraceInfo)
import Plutarch.Show (PShow, pshow)
import Plutarch.String (PString)

{- | Like Haskell's @traceShowId@ but for Plutarch, at the info level.

@since 1.6.0
-}
ptraceInfoShowId ::
  forall (a :: S -> Type) (s :: S).
  PShow a =>
  Term s a ->
  Term s a
ptraceInfoShowId :: forall (a :: S -> Type) (s :: S). PShow a => Term s a -> Term s a
ptraceInfoShowId Term s a
x = (Config -> Term s a) -> Term s a
forall (s :: S) (a :: S -> Type). (Config -> Term s a) -> Term s a
pgetConfig ((Config -> Term s a) -> Term s a)
-> (Config -> Term s a) -> Term s a
forall a b. (a -> b) -> a -> b
$ \case
  Config
NoTracing -> Term s a
x
  Tracing LogLevel
_ TracingMode
_ -> Term s PString -> Term s a -> Term s a
forall (a :: S -> Type) (s :: S).
Term s PString -> Term s a -> Term s a
ptraceInfo (Term s a -> Term s PString
forall (a :: S -> Type) (s :: S).
PShow a =>
Term s a -> Term s PString
pshow Term s a
x) Term s a
x

{- | Synonym for 'ptraceInfoShowId'.

@since 1.6.0
-}
ptraceShowId ::
  forall (a :: S -> Type) (s :: S).
  PShow a =>
  Term s a ->
  Term s a
ptraceShowId :: forall (a :: S -> Type) (s :: S). PShow a => Term s a -> Term s a
ptraceShowId = Term s a -> Term s a
forall (a :: S -> Type) (s :: S). PShow a => Term s a -> Term s a
ptraceInfoShowId
{-# DEPRECATED ptraceShowId "Use ptraceInfoShowId" #-}

{- | Like Haskell's @traceShowId@ but for Plutarch, at the debug level.

@since 1.6.0
-}
ptraceDebugShowId ::
  forall (a :: S -> Type) (s :: S).
  PShow a =>
  Term s a ->
  Term s a
ptraceDebugShowId :: forall (a :: S -> Type) (s :: S). PShow a => Term s a -> Term s a
ptraceDebugShowId Term s a
x = (Config -> Term s a) -> Term s a
forall (s :: S) (a :: S -> Type). (Config -> Term s a) -> Term s a
pgetConfig ((Config -> Term s a) -> Term s a)
-> (Config -> Term s a) -> Term s a
forall a b. (a -> b) -> a -> b
$ \case
  Config
NoTracing -> Term s a
x
  Config
_ -> Term s PString -> Term s a -> Term s a
forall (a :: S -> Type) (s :: S).
Term s PString -> Term s a -> Term s a
ptraceDebug (Term s a -> Term s PString
forall (a :: S -> Type) (s :: S).
PShow a =>
Term s a -> Term s PString
pshow Term s a
x) Term s a
x

{- | Trace the given message at the info level, then terminate with 'perror'.

@since 1.6.0
-}
ptraceInfoError ::
  forall (a :: S -> Type) (s :: S).
  Term s PString ->
  Term s a
ptraceInfoError :: forall (a :: S -> Type) (s :: S). Term s PString -> Term s a
ptraceInfoError = (Term s PString -> Term s a -> Term s a)
-> Term s a -> Term s PString -> Term s a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Term s PString -> Term s a -> Term s a
forall (a :: S -> Type) (s :: S).
Term s PString -> Term s a -> Term s a
ptraceInfo Term s a
forall (s :: S) (a :: S -> Type). Term s a
perror

{- | Synonym for 'ptraceInfoError'.

@since 1.6.0
-}
ptraceError ::
  forall (a :: S -> Type) (s :: S).
  Term s PString ->
  Term s a
ptraceError :: forall (a :: S -> Type) (s :: S). Term s PString -> Term s a
ptraceError = Term s PString -> Term s a
forall (a :: S -> Type) (s :: S). Term s PString -> Term s a
ptraceInfoError
{-# DEPRECATED ptraceError "Use ptraceInfoError" #-}

{- | Trace the given message at the debug level, then terminate with 'perror'.

@since 1.6.0
-}
ptraceDebugError ::
  forall (a :: S -> Type) (s :: S).
  Term s PString ->
  Term s a
ptraceDebugError :: forall (a :: S -> Type) (s :: S). Term s PString -> Term s a
ptraceDebugError = (Term s PString -> Term s a -> Term s a)
-> Term s a -> Term s PString -> Term s a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Term s PString -> Term s a -> Term s a
forall (a :: S -> Type) (s :: S).
Term s PString -> Term s a -> Term s a
ptraceDebug Term s a
forall (s :: S) (a :: S -> Type). Term s a
perror

{- | Trace the given message at the info level if the argument is true.

@since 1.6.0
-}
ptraceInfoIfTrue ::
  forall (s :: S).
  Term s PString ->
  Term s PBool ->
  Term s PBool
ptraceInfoIfTrue :: forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceInfoIfTrue Term s PString
msg Term s PBool
x = (Config -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type). (Config -> Term s a) -> Term s a
pgetConfig ((Config -> Term s PBool) -> Term s PBool)
-> (Config -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \case
  Config
NoTracing -> Term s PBool
x
  Config
_ -> Term s PBool -> (Term s PBool -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet Term s PBool
x ((Term s PBool -> Term s PBool) -> Term s PBool)
-> (Term s PBool -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \Term s PBool
x' -> Term s PBool -> Term s PBool -> Term s PBool -> Term s PBool
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif Term s PBool
x' (Term s (PString :--> (PBool :--> PBool))
forall (s :: S) (a :: S -> Type). Term s (PString :--> (a :--> a))
ptrace' Term s (PString :--> (PBool :--> PBool))
-> Term s PString -> Term s (PBool :--> PBool)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PString
msg Term s (PBool :--> PBool) -> Term s PBool -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PBool
x') Term s PBool
x'

{- | Synonym for 'ptraceInfoIfTrue'.

@since 1.6.0
-}
ptraceIfTrue ::
  forall (s :: S).
  Term s PString ->
  Term s PBool ->
  Term s PBool
ptraceIfTrue :: forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceIfTrue = Term s PString -> Term s PBool -> Term s PBool
forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceInfoIfTrue
{-# DEPRECATED ptraceIfTrue "Use ptraceInfoIfTrue" #-}

{- | Trace the given message at the debug level if the argument is true.

@since 1.6.0
-}
ptraceDebugIfTrue ::
  forall (s :: S).
  Term s PString ->
  Term s PBool ->
  Term s PBool
ptraceDebugIfTrue :: forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceDebugIfTrue Term s PString
msg Term s PBool
x = (Config -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type). (Config -> Term s a) -> Term s a
pgetConfig ((Config -> Term s PBool) -> Term s PBool)
-> (Config -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \case
  Config
NoTracing -> Term s PBool
x
  Tracing LogLevel
ll TracingMode
_ -> case LogLevel
ll of
    LogLevel
LogInfo -> Term s PBool
x
    LogLevel
LogDebug -> Term s PBool -> (Term s PBool -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet Term s PBool
x ((Term s PBool -> Term s PBool) -> Term s PBool)
-> (Term s PBool -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \Term s PBool
x' -> Term s PBool -> Term s PBool -> Term s PBool -> Term s PBool
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif Term s PBool
x' (Term s (PString :--> (PBool :--> PBool))
forall (s :: S) (a :: S -> Type). Term s (PString :--> (a :--> a))
ptrace' Term s (PString :--> (PBool :--> PBool))
-> Term s PString -> Term s (PBool :--> PBool)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PString
msg Term s (PBool :--> PBool) -> Term s PBool -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PBool
x') Term s PBool
x'

{- | Trace the given message at the info level if the argument is false.

@since 1.6.0
-}
ptraceInfoIfFalse ::
  forall (s :: S).
  Term s PString ->
  Term s PBool ->
  Term s PBool
ptraceInfoIfFalse :: forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceInfoIfFalse Term s PString
msg Term s PBool
x = (Config -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type). (Config -> Term s a) -> Term s a
pgetConfig ((Config -> Term s PBool) -> Term s PBool)
-> (Config -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \case
  Config
NoTracing -> Term s PBool
x
  Config
_ -> Term s PBool -> (Term s PBool -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet Term s PBool
x ((Term s PBool -> Term s PBool) -> Term s PBool)
-> (Term s PBool -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \Term s PBool
x' -> Term s PBool -> Term s PBool -> Term s PBool -> Term s PBool
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif Term s PBool
x' Term s PBool
x' (Term s (PString :--> (PBool :--> PBool))
forall (s :: S) (a :: S -> Type). Term s (PString :--> (a :--> a))
ptrace' Term s (PString :--> (PBool :--> PBool))
-> Term s PString -> Term s (PBool :--> PBool)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PString
msg Term s (PBool :--> PBool) -> Term s PBool -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PBool
x')

{- | Synonym for 'ptraceInfoIfFalse'.

@since 1.6.0
-}
ptraceIfFalse ::
  forall (s :: S).
  Term s PString ->
  Term s PBool ->
  Term s PBool
ptraceIfFalse :: forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceIfFalse = Term s PString -> Term s PBool -> Term s PBool
forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceInfoIfFalse
{-# DEPRECATED ptraceIfFalse "Use ptraceInfoIfFalse" #-}

{- | Trace the given message at the debug level if the argument is false.

@since 1.6.0
-}
ptraceDebugIfFalse ::
  forall (s :: S).
  Term s PString ->
  Term s PBool ->
  Term s PBool
ptraceDebugIfFalse :: forall (s :: S). Term s PString -> Term s PBool -> Term s PBool
ptraceDebugIfFalse Term s PString
msg Term s PBool
x = (Config -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type). (Config -> Term s a) -> Term s a
pgetConfig ((Config -> Term s PBool) -> Term s PBool)
-> (Config -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \case
  Config
NoTracing -> Term s PBool
x
  Tracing LogLevel
ll TracingMode
_ -> case LogLevel
ll of
    LogLevel
LogInfo -> Term s PBool
x
    LogLevel
LogDebug -> Term s PBool -> (Term s PBool -> Term s PBool) -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet Term s PBool
x ((Term s PBool -> Term s PBool) -> Term s PBool)
-> (Term s PBool -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \Term s PBool
x' -> Term s PBool -> Term s PBool -> Term s PBool -> Term s PBool
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif Term s PBool
x' Term s PBool
x' (Term s (PString :--> (PBool :--> PBool))
forall (s :: S) (a :: S -> Type). Term s (PString :--> (a :--> a))
ptrace' Term s (PString :--> (PBool :--> PBool))
-> Term s PString -> Term s (PBool :--> PBool)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PString
msg Term s (PBool :--> PBool) -> Term s PBool -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PBool
x')