-- | 'Tweak's working on the minting part of a 'TxSkel'
module Cooked.Tweak.Mint
  ( addMintTweak,
    removeMintTweak,
  )
where

import Cooked.Skeleton
import Cooked.Tweak.Common
import Data.List (partition)
import Optics.Core

-- | Adds a new entry to the 'TxSkelMints' of the transaction skeleton under
-- modification. As this is implemented in terms of 'addMint', the same caveats
-- apply as do to that function!
addMintTweak :: (MonadTweak m) => Mint -> m ()
addMintTweak :: forall (m :: * -> *). MonadTweak m => Mint -> m ()
addMintTweak = Optic' A_Lens NoIx TxSkel TxSkelMints
-> (TxSkelMints -> TxSkelMints) -> 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 TxSkelMints
txSkelMintsL ((TxSkelMints -> TxSkelMints) -> m ())
-> (Mint -> TxSkelMints -> TxSkelMints) -> Mint -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxSkelMints -> Mint -> TxSkelMints)
-> Mint -> TxSkelMints -> TxSkelMints
forall a b c. (a -> b -> c) -> b -> a -> c
flip TxSkelMints -> Mint -> TxSkelMints
addMint

-- | Remove some entries from the 'TxSkelMints' of a transaction, according to
-- some predicate. The returned list holds the removed entries.
removeMintTweak :: (MonadTweak m) => (Mint -> Bool) -> m [Mint]
removeMintTweak :: forall (m :: * -> *). MonadTweak m => (Mint -> Bool) -> m [Mint]
removeMintTweak Mint -> Bool
removePred = do
  [Mint]
presentMints <- Optic' A_Getter NoIx TxSkel [Mint] -> m [Mint]
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Getter) =>
Optic' k is TxSkel a -> m a
viewTweak (Optic' A_Getter NoIx TxSkel [Mint] -> m [Mint])
-> Optic' A_Getter NoIx TxSkel [Mint] -> m [Mint]
forall a b. (a -> b) -> a -> b
$ Optic' A_Lens NoIx TxSkel TxSkelMints
txSkelMintsL Optic' A_Lens NoIx TxSkel TxSkelMints
-> Optic A_Getter NoIx TxSkelMints TxSkelMints [Mint] [Mint]
-> Optic' A_Getter NoIx TxSkel [Mint]
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% (TxSkelMints -> [Mint])
-> Optic A_Getter NoIx TxSkelMints TxSkelMints [Mint] [Mint]
forall s a. (s -> a) -> Getter s a
to TxSkelMints -> [Mint]
txSkelMintsToList
  let ([Mint]
removed, [Mint]
kept) = (Mint -> Bool) -> [Mint] -> ([Mint], [Mint])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Mint -> Bool
removePred [Mint]
presentMints
  Optic' A_Lens NoIx TxSkel TxSkelMints -> TxSkelMints -> m ()
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Setter) =>
Optic' k is TxSkel a -> a -> m ()
setTweak Optic' A_Lens NoIx TxSkel TxSkelMints
txSkelMintsL (TxSkelMints -> m ()) -> TxSkelMints -> m ()
forall a b. (a -> b) -> a -> b
$ [Mint] -> TxSkelMints
txSkelMintsFromList [Mint]
kept
  [Mint] -> m [Mint]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Mint]
removed