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

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

-- | Adds new entries to the 'TxSkelMints' of the transaction skeleton under
-- modification.
addMintsTweak :: (MonadTweak m) => [Mint] -> m ()
addMintsTweak :: forall (m :: * -> *). MonadTweak m => [Mint] -> m ()
addMintsTweak [Mint]
newMints = Optic' A_Lens NoIx TxSkel [Mint] -> ([Mint] -> [Mint]) -> m ()
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Setter) =>
Optic' k is TxSkel a -> (a -> a) -> m ()
overTweak (Lens' TxSkel TxSkelMints
txSkelMintsL Lens' TxSkel TxSkelMints
-> Optic An_Iso NoIx TxSkelMints TxSkelMints [Mint] [Mint]
-> Optic' A_Lens 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
% Optic An_Iso NoIx TxSkelMints TxSkelMints [Mint] [Mint]
txSkelMintsListI) ([Mint] -> [Mint] -> [Mint]
forall a. [a] -> [a] -> [a]
++ [Mint]
newMints)

-- | 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_Lens 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_Lens NoIx TxSkel [Mint] -> m [Mint])
-> Optic' A_Lens NoIx TxSkel [Mint] -> m [Mint]
forall a b. (a -> b) -> a -> b
$ Lens' TxSkel TxSkelMints
txSkelMintsL Lens' TxSkel TxSkelMints
-> Optic An_Iso NoIx TxSkelMints TxSkelMints [Mint] [Mint]
-> Optic' A_Lens 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
% Optic An_Iso NoIx TxSkelMints TxSkelMints [Mint] [Mint]
txSkelMintsListI
  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 [Mint] -> [Mint] -> m ()
forall (m :: * -> *) k (is :: IxList) a.
(MonadTweak m, Is k A_Setter) =>
Optic' k is TxSkel a -> a -> m ()
setTweak (Lens' TxSkel TxSkelMints
txSkelMintsL Lens' TxSkel TxSkelMints
-> Optic An_Iso NoIx TxSkelMints TxSkelMints [Mint] [Mint]
-> Optic' A_Lens 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
% Optic An_Iso NoIx TxSkelMints TxSkelMints [Mint] [Mint]
txSkelMintsListI) [Mint]
kept
  [Mint] -> m [Mint]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Mint]
removed