{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.CLI.EraIndependent.Debug.TransactionView.Run
  ( runTransactionViewCmd
  )
where

import Cardano.Api

import Cardano.CLI.Compatible.Exception
import Cardano.CLI.Compatible.Json.Friendly
  ( friendlyTx
  , friendlyTxBody
  )
import Cardano.CLI.EraIndependent.Debug.TransactionView.Command
import Cardano.CLI.Read
import Cardano.CLI.Type.Common

runTransactionViewCmd
  :: ()
  => TransactionViewCmdArgs
  -> CIO e ()
runTransactionViewCmd :: forall e. TransactionViewCmdArgs -> CIO e ()
runTransactionViewCmd
  TransactionViewCmdArgs
    { Vary '[FormatJson, FormatYaml]
outputFormat :: Vary '[FormatJson, FormatYaml]
outputFormat :: TransactionViewCmdArgs -> Vary '[FormatJson, FormatYaml]
outputFormat
    , Maybe (File () 'Out)
mOutFile :: Maybe (File () 'Out)
mOutFile :: TransactionViewCmdArgs -> Maybe (File () 'Out)
mOutFile
    , InputTxBodyOrTxFile
inputTxBodyOrTxFile :: InputTxBodyOrTxFile
inputTxBodyOrTxFile :: TransactionViewCmdArgs -> InputTxBodyOrTxFile
inputTxBodyOrTxFile
    } =
    case InputTxBodyOrTxFile
inputTxBodyOrTxFile of
      InputTxBodyFile (File FilePath
txbodyFilePath) -> do
        txbodyFile <- IO FileOrPipe -> RIO e FileOrPipe
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileOrPipe -> RIO e FileOrPipe)
-> IO FileOrPipe -> RIO e FileOrPipe
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileOrPipe
fileOrPipe FilePath
txbodyFilePath
        unwitnessed <-
          fromEitherIOCli $
            readFileTxBody txbodyFile
        InAnyShelleyBasedEra era txbody <- pure $ unIncompleteTxBody unwitnessed
        -- Why are we differentiating between a transaction body and a transaction?
        -- In the case of a transaction body, we /could/ simply call @makeSignedTransaction []@
        -- to get a transaction which would allow us to reuse friendlyTxBS. However,
        -- this would mean that we'd have an empty list of witnesses mentioned in the output, which
        -- is arguably not part of the transaction body.
        fromEitherIOCli @(FileError ()) $
          friendlyTxBody outputFormat mOutFile era txbody
      InputTxFile (File FilePath
txFilePath) -> do
        txFile <- IO FileOrPipe -> RIO e FileOrPipe
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileOrPipe -> RIO e FileOrPipe)
-> IO FileOrPipe -> RIO e FileOrPipe
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileOrPipe
fileOrPipe FilePath
txFilePath
        InAnyShelleyBasedEra era tx <- fromEitherIOCli (readFileTx txFile)
        fromEitherIOCli @(FileError ()) $
          friendlyTx outputFormat mOutFile era tx