{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.CLI.Orphans
  (
  )
where

import           Cardano.Api
import qualified Cardano.Api.Experimental as Exp
import qualified Cardano.Api.Ledger as L
import           Cardano.Api.Shelley (scriptDataToJsonDetailedSchema)

import           Data.Aeson

-- TODO upstream this orphaned instance to the ledger
instance (L.EraTxOut ledgerera, L.EraGov ledgerera) => ToJSON (L.NewEpochState ledgerera) where
  toJSON :: NewEpochState ledgerera -> Value
toJSON (L.NewEpochState EpochNo
nesEL BlocksMade (EraCrypto ledgerera)
nesBprev BlocksMade (EraCrypto ledgerera)
nesBCur EpochState ledgerera
nesEs StrictMaybe (PulsingRewUpdate (EraCrypto ledgerera))
nesRu PoolDistr (EraCrypto ledgerera)
nesPd StashedAVVMAddresses ledgerera
_stashedAvvm) =
    [Pair] -> Value
object
      [ Key
"currentEpoch" Key -> EpochNo -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= EpochNo
nesEL
      , Key
"priorBlocks" Key -> BlocksMade (EraCrypto ledgerera) -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= BlocksMade (EraCrypto ledgerera)
nesBprev
      , Key
"currentEpochBlocks" Key -> BlocksMade (EraCrypto ledgerera) -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= BlocksMade (EraCrypto ledgerera)
nesBCur
      , Key
"currentEpochState" Key -> EpochState ledgerera -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= EpochState ledgerera
nesEs
      , Key
"rewardUpdate" Key -> StrictMaybe (PulsingRewUpdate (EraCrypto ledgerera)) -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= StrictMaybe (PulsingRewUpdate (EraCrypto ledgerera))
nesRu
      , Key
"currentStakeDistribution" Key -> PoolDistr (EraCrypto ledgerera) -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= PoolDistr (EraCrypto ledgerera)
nesPd
      ]

instance ToJSON HashableScriptData where
  toJSON :: HashableScriptData -> Value
toJSON HashableScriptData
hsd =
    [Pair] -> Value
object
      [ Key
"hash" Key -> Hash ScriptData -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= HashableScriptData -> Hash ScriptData
hashScriptDataBytes HashableScriptData
hsd
      , Key
"json" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= HashableScriptData -> Value
scriptDataToJsonDetailedSchema HashableScriptData
hsd
      ]

-- TODO Delete me when added in API
instance Convert Exp.Era MaryEraOnwards where
  convert :: forall era. Era era -> MaryEraOnwards era
convert = \case
    Era era
Exp.BabbageEra -> MaryEraOnwards era
MaryEraOnwards BabbageEra
MaryEraOnwardsBabbage
    Era era
Exp.ConwayEra -> MaryEraOnwards era
MaryEraOnwards ConwayEra
MaryEraOnwardsConway