content: {-# LANGUAGE PatternSynonyms #-}
  {-# LANGUAGE TypeFamilies #-}
  {-# OPTIONS_GHC -Wno-orphans #-}
  
  module Cardano.Ledger.Dijkstra.UTxO (
    getDijkstraScriptsNeeded,
  ) where
  
  import Cardano.Ledger.Alonzo.UTxO (
    AlonzoEraUTxO (..),
    AlonzoScriptsNeeded (..),
    getAlonzoScriptsHashesNeeded,
    zipAsIxItem,
   )
  import Cardano.Ledger.Babbage.UTxO (
    getBabbageScriptsProvided,
    getBabbageSpendingDatum,
    getBabbageSupplementalDataHashes,
   )
  import Cardano.Ledger.Conway.UTxO (
    conwayConsumed,
    conwayProducedValue,
    getConwayMinFeeTxUtxo,
    getConwayScriptsNeeded,
    getConwayWitsVKeyNeeded,
   )
  import Cardano.Ledger.Credential (credScriptHash)
  import Cardano.Ledger.Dijkstra.Core (AsIxItem (..), EraTxBody (..))
  import Cardano.Ledger.Dijkstra.Era (DijkstraEra)
  import Cardano.Ledger.Dijkstra.Scripts (DijkstraEraScript (..), pattern GuardingPurpose)
  import Cardano.Ledger.Dijkstra.State (EraUTxO (..), UTxO)
  import Cardano.Ledger.Dijkstra.State.CertState ()
  import Cardano.Ledger.Dijkstra.Tx ()
  import Cardano.Ledger.Dijkstra.TxBody (DijkstraEraTxBody (..))
  import Cardano.Ledger.Mary.UTxO (getConsumedMaryValue)
  import Data.Maybe (catMaybes)
  import Lens.Micro ((^.))
  
  instance EraUTxO DijkstraEra where
    type ScriptsNeeded DijkstraEra = AlonzoScriptsNeeded DijkstraEra
  
    consumed = conwayConsumed
  
    getConsumedValue = getConsumedMaryValue
  
    getProducedValue = conwayProducedValue
  
    getScriptsProvided = getBabbageScriptsProvided
  
    getScriptsNeeded = getDijkstraScriptsNeeded
  
    getScriptsHashesNeeded = getAlonzoScriptsHashesNeeded
  
    getWitsVKeyNeeded _ = getConwayWitsVKeyNeeded
  
    getMinFeeTxUtxo = getConwayMinFeeTxUtxo
  
  getDijkstraScriptsNeeded ::
    (DijkstraEraTxBody era, DijkstraEraScript era) => UTxO era -> TxBody era -> AlonzoScriptsNeeded era
  getDijkstraScriptsNeeded utxo txb =
    getConwayScriptsNeeded utxo txb
      <> guardingScriptsNeeded
    where
      guardingScriptsNeeded = AlonzoScriptsNeeded $
        catMaybes $
          zipAsIxItem (txb ^. guardsTxBodyL) $
            \(AsIxItem idx cred) -> (\sh -> (GuardingPurpose (AsIxItem idx sh), sh)) <$> credScriptHash cred
  
  instance AlonzoEraUTxO DijkstraEra where
    getSupplementalDataHashes = getBabbageSupplementalDataHashes
  
    getSpendingDatum = getBabbageSpendingDatum
  