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

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

-- | 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

-- | 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
label = do
  x -> m Bool
forall (m :: * -> *) x.
(MonadTweak m, LabelConstrs x) =>
x -> m Bool
hasLabelTweak x
label m Bool -> (Bool -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard
  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
label