-- | This module provides tweaks operating on transaction labels
module Cooked.Tweak.Labels
  ( labelled,
    labelled',
    addLabelTweak,
    removeLabelTweak,
    hasLabelTweak,
    ensureLabelTweak,
  )
where

import Control.Monad
import Cooked.Skeleton
import Cooked.Tweak.Common
import Data.Functor
import Data.Set qualified as Set
import Data.Text (Text)

-- | Adds a label to a 'TxSkel'.
addLabelTweak :: (MonadTweak m, LabelConstrs x) => x -> m ()
addLabelTweak :: forall (m :: * -> *) x. (MonadTweak m, LabelConstrs x) => x -> m ()
addLabelTweak = Optic' A_Lens NoIx TxSkel (Set TxSkelLabel)
-> (Set TxSkelLabel -> Set TxSkelLabel) -> m ()
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Setter) =>
Optic' k is TxSkel a -> (a -> a) -> m ()
overTweak Optic' A_Lens NoIx TxSkel (Set TxSkelLabel)
txSkelLabelL ((Set TxSkelLabel -> Set TxSkelLabel) -> m ())
-> (x -> Set TxSkelLabel -> Set TxSkelLabel) -> x -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxSkelLabel -> Set TxSkelLabel -> Set TxSkelLabel
forall a. Ord a => a -> Set a -> Set a
Set.insert (TxSkelLabel -> Set TxSkelLabel -> Set TxSkelLabel)
-> (x -> TxSkelLabel) -> x -> Set TxSkelLabel -> Set TxSkelLabel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> TxSkelLabel
forall x. LabelConstrs x => x -> TxSkelLabel
TxSkelLabel

-- | Checks if a given label is present in the 'TxSkel'
hasLabelTweak :: (MonadTweak m, LabelConstrs x) => x -> m Bool
hasLabelTweak :: forall (m :: * -> *) x.
(MonadTweak m, LabelConstrs x) =>
x -> m Bool
hasLabelTweak = (Optic' A_Lens NoIx TxSkel (Set TxSkelLabel) -> m (Set TxSkelLabel)
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Getter) =>
Optic' k is TxSkel a -> m a
viewTweak Optic' A_Lens NoIx TxSkel (Set TxSkelLabel)
txSkelLabelL m (Set TxSkelLabel) -> (Set TxSkelLabel -> Bool) -> m Bool
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>) ((Set TxSkelLabel -> Bool) -> m Bool)
-> (x -> Set TxSkelLabel -> Bool) -> x -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxSkelLabel -> Set TxSkelLabel -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member (TxSkelLabel -> Set TxSkelLabel -> Bool)
-> (x -> TxSkelLabel) -> x -> Set TxSkelLabel -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> TxSkelLabel
forall x. LabelConstrs x => x -> TxSkelLabel
TxSkelLabel

-- | Ensures a given label is present in the 'TxSkel'
ensureLabelTweak :: (MonadTweak m, LabelConstrs x) => x -> m ()
ensureLabelTweak :: forall (m :: * -> *) x. (MonadTweak m, LabelConstrs x) => x -> m ()
ensureLabelTweak = x -> m Bool
forall (m :: * -> *) x.
(MonadTweak m, LabelConstrs x) =>
x -> m Bool
hasLabelTweak (x -> m Bool) -> (Bool -> m ()) -> x -> m ()
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Bool -> m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard

-- | Removes a label from a 'TxSkel' when possible, fails otherwise
removeLabelTweak :: (MonadTweak m, LabelConstrs x) => x -> m ()
removeLabelTweak :: forall (m :: * -> *) x. (MonadTweak m, LabelConstrs x) => x -> m ()
removeLabelTweak x
lbl = do
  x -> m ()
forall (m :: * -> *) x. (MonadTweak m, LabelConstrs x) => x -> m ()
ensureLabelTweak x
lbl
  Optic' A_Lens NoIx TxSkel (Set TxSkelLabel)
-> (Set TxSkelLabel -> Set TxSkelLabel) -> m ()
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Setter) =>
Optic' k is TxSkel a -> (a -> a) -> m ()
overTweak Optic' A_Lens NoIx TxSkel (Set TxSkelLabel)
txSkelLabelL ((Set TxSkelLabel -> Set TxSkelLabel) -> m ())
-> (TxSkelLabel -> Set TxSkelLabel -> Set TxSkelLabel)
-> TxSkelLabel
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxSkelLabel -> Set TxSkelLabel -> Set TxSkelLabel
forall a. Ord a => a -> Set a -> Set a
Set.delete (TxSkelLabel -> m ()) -> TxSkelLabel -> m ()
forall a b. (a -> b) -> a -> b
$ x -> TxSkelLabel
forall x. LabelConstrs x => x -> TxSkelLabel
TxSkelLabel x
lbl

-- | `labelled'` specialised to Text labels.
--
-- >
-- > someEndpoint = do
-- >   ...
-- >   validateTxSkel' txSkelTemplate
-- >      { txSkelLabels =
-- >         [ "InitialMinting"
-- >         , "AuctionWorkflow"
-- >         , "Spending"
-- >         , label SomeLabelType]
-- >      }
-- >
-- > someTest = someEndpoint & somewhere (labelled "Spending" doubleSatAttack)
labelled :: (MonadTweak m) => Text -> m a -> m a
labelled :: forall (m :: * -> *) a. MonadTweak m => Text -> m a -> m a
labelled = Text -> m a -> m a
forall (m :: * -> *) lbl a.
(MonadTweak m, LabelConstrs lbl) =>
lbl -> m a -> m a
labelled'

-- | Apply a tweak to a given transaction if it has a specific label. This
-- can be useful to apply a tweak to every (or any) transaction in a set of
-- associated transactions.
--
-- >
-- > someEndpoint = do
-- >   ...
-- >   validateTxSkel' txSkelTemplate
-- >      { txSkelLabels =
-- >         [ "InitialMinting"
-- >         , "AuctionWorkflow"
-- >         , label SomeLabelType]
-- >      }
-- >
-- > someTest = someEndpoint & eveywhere (labelled' SomeLabelType someTweak)
-- > anotherTest = someEndpoint & somewhere (labelled' SomeLabelType someTweak)
labelled' :: (MonadTweak m, LabelConstrs lbl) => lbl -> m a -> m a
labelled' :: forall (m :: * -> *) lbl a.
(MonadTweak m, LabelConstrs lbl) =>
lbl -> m a -> m a
labelled' lbl
lbl = (lbl -> m ()
forall (m :: * -> *) x. (MonadTweak m, LabelConstrs x) => x -> m ()
ensureLabelTweak lbl
lbl m () -> m a -> m a
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)