{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

module Cardano.Api.Internal.LedgerEvents.Rule.BBODY.UTXOW
  ( handleAlonzoUTxOWEvent
  , handleAllegraMaryUTxOWEvent
  , handleConwayUTxOWEvent
  , handlePreAlonzoUTxOWEvent
  )
where

import Cardano.Api.Internal.LedgerEvents.LedgerEvent

import Cardano.Ledger.Allegra.Rules qualified as Allegra
import Cardano.Ledger.Alonzo.Rules
  ( AlonzoUtxoEvent (..)
  , AlonzoUtxosEvent (..)
  , AlonzoUtxowEvent (..)
  )
import Cardano.Ledger.Alonzo.Rules qualified as Alonzo
import Cardano.Ledger.Conway
import Cardano.Ledger.Conway.Rules qualified as Conway
import Cardano.Ledger.Core qualified as Ledger.Core
import Cardano.Ledger.Shelley.Rules qualified as Shelley

import Control.State.Transition.Extended

handleConwayUTxOWEvent
  :: AlonzoUtxowEvent ConwayEra -> Maybe LedgerEvent
handleConwayUTxOWEvent :: AlonzoUtxowEvent ConwayEra -> Maybe LedgerEvent
handleConwayUTxOWEvent (Alonzo.WrappedShelleyEraEvent (Shelley.UtxoEvent (Alonzo.UtxosEvent Event (EraRule "UTXOS" ConwayEra)
conwayUTxOsEvent))) =
  case Event (EraRule "UTXOS" ConwayEra)
conwayUTxOsEvent of
    Conway.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Conway.SuccessfulPlutusScriptsEvent NonEmpty PlutusWithContext
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty PlutusWithContext -> LedgerEvent
SuccessfulPlutusScript NonEmpty PlutusWithContext
e
    Conway.FailedPlutusScriptsEvent NonEmpty PlutusWithContext
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty PlutusWithContext -> LedgerEvent
FailedPlutusScript NonEmpty PlutusWithContext
e
    Conway.TxUTxODiff UTxO ConwayEra
_ UTxO ConwayEra
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing

handleAlonzoUTxOWEvent
  :: Event (Ledger.Core.EraRule "UTXO" ledgerera) ~ AlonzoUtxoEvent ledgerera
  => Event (Ledger.Core.EraRule "UTXOS" ledgerera) ~ AlonzoUtxosEvent ledgerera
  => AlonzoUtxowEvent ledgerera -> Maybe LedgerEvent
handleAlonzoUTxOWEvent :: forall ledgerera.
(Event (EraRule "UTXO" ledgerera) ~ AlonzoUtxoEvent ledgerera,
 Event (EraRule "UTXOS" ledgerera) ~ AlonzoUtxosEvent ledgerera) =>
AlonzoUtxowEvent ledgerera -> Maybe LedgerEvent
handleAlonzoUTxOWEvent (WrappedShelleyEraEvent (Shelley.UtxoEvent (UtxosEvent Event (EraRule "UTXOS" ledgerera)
utxoEvent))) =
  case Event (EraRule "UTXOS" ledgerera)
utxoEvent of
    Alonzo.AlonzoPpupToUtxosEvent{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Alonzo.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Alonzo.SuccessfulPlutusScriptsEvent NonEmpty PlutusWithContext
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty PlutusWithContext -> LedgerEvent
SuccessfulPlutusScript NonEmpty PlutusWithContext
e
    Alonzo.FailedPlutusScriptsEvent NonEmpty PlutusWithContext
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty PlutusWithContext -> LedgerEvent
FailedPlutusScript NonEmpty PlutusWithContext
e
    Alonzo.TxUTxODiff UTxO ledgerera
_ UTxO ledgerera
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing

handlePreAlonzoUTxOWEvent
  :: Event (Ledger.Core.EraRule "UTXO" ledgerera) ~ Shelley.UtxoEvent ledgerera
  => Event (Ledger.Core.EraRule "PPUP" ledgerera) ~ Shelley.PpupEvent ledgerera
  => Shelley.ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handlePreAlonzoUTxOWEvent :: forall ledgerera.
(Event (EraRule "UTXO" ledgerera) ~ UtxoEvent ledgerera,
 Event (EraRule "PPUP" ledgerera) ~ PpupEvent ledgerera) =>
ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handlePreAlonzoUTxOWEvent (Shelley.UtxoEvent Event (EraRule "UTXO" ledgerera)
e) =
  case Event (EraRule "UTXO" ledgerera)
e of
    Shelley.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Shelley.UpdateEvent (Shelley.PpupNewEpoch EpochNo
_) -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Shelley.TxUTxODiff UTxO ledgerera
_ UTxO ledgerera
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing

handleAllegraMaryUTxOWEvent
  :: Event (Ledger.Core.EraRule "UTXO" ledgerera) ~ Allegra.AllegraUtxoEvent ledgerera
  => Event (Ledger.Core.EraRule "PPUP" ledgerera) ~ Shelley.PpupEvent ledgerera
  => Shelley.ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handleAllegraMaryUTxOWEvent :: forall ledgerera.
(Event (EraRule "UTXO" ledgerera) ~ AllegraUtxoEvent ledgerera,
 Event (EraRule "PPUP" ledgerera) ~ PpupEvent ledgerera) =>
ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handleAllegraMaryUTxOWEvent (Shelley.UtxoEvent Event (EraRule "UTXO" ledgerera)
e) =
  case Event (EraRule "UTXO" ledgerera)
e of
    Allegra.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Allegra.UpdateEvent (Shelley.PpupNewEpoch EpochNo
_) -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Allegra.TxUTxODiff UTxO ledgerera
_ UTxO ledgerera
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing