{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Compatible.Governance
  ( CompatibleGovernanceCmds (..)
  , pCompatibleGovernanceCmds
  , renderCompatibleGovernanceCmds
  , runCompatibleGovernanceCmds
  )
where

import           Cardano.Api

import           Cardano.CLI.EraBased.Options.Governance
import           Cardano.CLI.EraBased.Run.Governance
import           Cardano.CLI.Types.Errors.CmdError

import           Data.Foldable
import           Data.Maybe
import           Data.Text
import           Options.Applicative

pCompatibleGovernanceCmds :: ShelleyBasedEra era -> Parser (CompatibleGovernanceCmds era)
pCompatibleGovernanceCmds :: forall era.
ShelleyBasedEra era -> Parser (CompatibleGovernanceCmds era)
pCompatibleGovernanceCmds ShelleyBasedEra era
sbe =
  [Parser (CompatibleGovernanceCmds era)]
-> Parser (CompatibleGovernanceCmds era)
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ([Parser (CompatibleGovernanceCmds era)]
 -> Parser (CompatibleGovernanceCmds era))
-> [Parser (CompatibleGovernanceCmds era)]
-> Parser (CompatibleGovernanceCmds era)
forall a b. (a -> b) -> a -> b
$ [Maybe (Parser (CompatibleGovernanceCmds era))]
-> [Parser (CompatibleGovernanceCmds era)]
forall a. [Maybe a] -> [a]
catMaybes [(GovernanceCmds era -> CompatibleGovernanceCmds era)
-> Parser (GovernanceCmds era)
-> Parser (CompatibleGovernanceCmds era)
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GovernanceCmds era -> CompatibleGovernanceCmds era
forall era. GovernanceCmds era -> CompatibleGovernanceCmds era
CreateCompatibleProtocolUpdateCmd (Parser (GovernanceCmds era)
 -> Parser (CompatibleGovernanceCmds era))
-> Maybe (Parser (GovernanceCmds era))
-> Maybe (Parser (CompatibleGovernanceCmds era))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShelleyBasedEra era -> Maybe (Parser (GovernanceCmds era))
forall era.
ShelleyBasedEra era -> Maybe (Parser (GovernanceCmds era))
pGovernanceCmds ShelleyBasedEra era
sbe]

-- TODO: After QA confirmms that the new compatibility commands meet their needs
-- we can remove all remaining legacy commands. We can also remove/move the exising
-- byron era commands under the new compatiblilty commands.
newtype CompatibleGovernanceCmds era
  = CreateCompatibleProtocolUpdateCmd (GovernanceCmds era)

runCompatibleGovernanceCmds :: CompatibleGovernanceCmds era -> ExceptT CmdError IO ()
runCompatibleGovernanceCmds :: forall era. CompatibleGovernanceCmds era -> ExceptT CmdError IO ()
runCompatibleGovernanceCmds = \case
  CreateCompatibleProtocolUpdateCmd GovernanceCmds era
cmd -> GovernanceCmds era -> ExceptT CmdError IO ()
forall era. GovernanceCmds era -> ExceptT CmdError IO ()
runGovernanceCmds GovernanceCmds era
cmd

renderCompatibleGovernanceCmds :: CompatibleGovernanceCmds era -> Text
renderCompatibleGovernanceCmds :: forall era. CompatibleGovernanceCmds era -> Text
renderCompatibleGovernanceCmds = \case
  CreateCompatibleProtocolUpdateCmd GovernanceCmds era
cmd -> GovernanceCmds era -> Text
forall era. GovernanceCmds era -> Text
renderGovernanceCmds GovernanceCmds era
cmd