{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}

module Cardano.CLI.EraBased.Script.Certificate.Types
  ( CertificateScriptWitness (..)
  , CliCertificateScriptRequirements (..)
  , PlutusScriptCliArgs (..)
  , PlutusRefScriptCliArgs (..)
  , createSimpleOrPlutusScriptFromCliArgs
  , createPlutusReferenceScriptFromCliArgs
  )
where

import           Cardano.Api

import           Cardano.CLI.Types.Common (ScriptDataOrFile)

newtype CertificateScriptWitness era
  = CertificateScriptWitness {forall era.
CertificateScriptWitness era -> ScriptWitness WitCtxStake era
cswScriptWitness :: ScriptWitness WitCtxStake era}
  deriving Int -> CertificateScriptWitness era -> ShowS
[CertificateScriptWitness era] -> ShowS
CertificateScriptWitness era -> String
(Int -> CertificateScriptWitness era -> ShowS)
-> (CertificateScriptWitness era -> String)
-> ([CertificateScriptWitness era] -> ShowS)
-> Show (CertificateScriptWitness era)
forall era. Int -> CertificateScriptWitness era -> ShowS
forall era. [CertificateScriptWitness era] -> ShowS
forall era. CertificateScriptWitness era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall era. Int -> CertificateScriptWitness era -> ShowS
showsPrec :: Int -> CertificateScriptWitness era -> ShowS
$cshow :: forall era. CertificateScriptWitness era -> String
show :: CertificateScriptWitness era -> String
$cshowList :: forall era. [CertificateScriptWitness era] -> ShowS
showList :: [CertificateScriptWitness era] -> ShowS
Show

data CliCertificateScriptRequirements
  = OnDiskPlutusScript PlutusScriptCliArgs
  | OnDiskSimpleScript (File ScriptInAnyLang In)
  | OnDiskPlutusRefScript PlutusRefScriptCliArgs
  deriving Int -> CliCertificateScriptRequirements -> ShowS
[CliCertificateScriptRequirements] -> ShowS
CliCertificateScriptRequirements -> String
(Int -> CliCertificateScriptRequirements -> ShowS)
-> (CliCertificateScriptRequirements -> String)
-> ([CliCertificateScriptRequirements] -> ShowS)
-> Show CliCertificateScriptRequirements
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CliCertificateScriptRequirements -> ShowS
showsPrec :: Int -> CliCertificateScriptRequirements -> ShowS
$cshow :: CliCertificateScriptRequirements -> String
show :: CliCertificateScriptRequirements -> String
$cshowList :: [CliCertificateScriptRequirements] -> ShowS
showList :: [CliCertificateScriptRequirements] -> ShowS
Show

data PlutusScriptCliArgs
  = OnDiskPlutusScriptCliArgs
      (File ScriptInAnyLang In)
      ScriptDataOrFile
      -- ^ Redeemer
      ExecutionUnits
  deriving Int -> PlutusScriptCliArgs -> ShowS
[PlutusScriptCliArgs] -> ShowS
PlutusScriptCliArgs -> String
(Int -> PlutusScriptCliArgs -> ShowS)
-> (PlutusScriptCliArgs -> String)
-> ([PlutusScriptCliArgs] -> ShowS)
-> Show PlutusScriptCliArgs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PlutusScriptCliArgs -> ShowS
showsPrec :: Int -> PlutusScriptCliArgs -> ShowS
$cshow :: PlutusScriptCliArgs -> String
show :: PlutusScriptCliArgs -> String
$cshowList :: [PlutusScriptCliArgs] -> ShowS
showList :: [PlutusScriptCliArgs] -> ShowS
Show

createSimpleOrPlutusScriptFromCliArgs
  :: File ScriptInAnyLang In
  -> Maybe (ScriptDataOrFile, ExecutionUnits)
  -> CliCertificateScriptRequirements
createSimpleOrPlutusScriptFromCliArgs :: File ScriptInAnyLang 'In
-> Maybe (ScriptDataOrFile, ExecutionUnits)
-> CliCertificateScriptRequirements
createSimpleOrPlutusScriptFromCliArgs File ScriptInAnyLang 'In
scriptFp (Just (ScriptDataOrFile
redeemer, ExecutionUnits
execUnits)) =
  PlutusScriptCliArgs -> CliCertificateScriptRequirements
OnDiskPlutusScript (PlutusScriptCliArgs -> CliCertificateScriptRequirements)
-> PlutusScriptCliArgs -> CliCertificateScriptRequirements
forall a b. (a -> b) -> a -> b
$ File ScriptInAnyLang 'In
-> ScriptDataOrFile -> ExecutionUnits -> PlutusScriptCliArgs
OnDiskPlutusScriptCliArgs File ScriptInAnyLang 'In
scriptFp ScriptDataOrFile
redeemer ExecutionUnits
execUnits
createSimpleOrPlutusScriptFromCliArgs File ScriptInAnyLang 'In
scriptFp Maybe (ScriptDataOrFile, ExecutionUnits)
Nothing =
  File ScriptInAnyLang 'In -> CliCertificateScriptRequirements
OnDiskSimpleScript File ScriptInAnyLang 'In
scriptFp

data PlutusRefScriptCliArgs
  = PlutusRefScriptCliArgs
      TxIn
      -- ^ TxIn with reference script
      AnyPlutusScriptVersion
      ScriptDataOrFile
      -- ^ Redeemer
      ExecutionUnits
  deriving Int -> PlutusRefScriptCliArgs -> ShowS
[PlutusRefScriptCliArgs] -> ShowS
PlutusRefScriptCliArgs -> String
(Int -> PlutusRefScriptCliArgs -> ShowS)
-> (PlutusRefScriptCliArgs -> String)
-> ([PlutusRefScriptCliArgs] -> ShowS)
-> Show PlutusRefScriptCliArgs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PlutusRefScriptCliArgs -> ShowS
showsPrec :: Int -> PlutusRefScriptCliArgs -> ShowS
$cshow :: PlutusRefScriptCliArgs -> String
show :: PlutusRefScriptCliArgs -> String
$cshowList :: [PlutusRefScriptCliArgs] -> ShowS
showList :: [PlutusRefScriptCliArgs] -> ShowS
Show

createPlutusReferenceScriptFromCliArgs
  :: TxIn
  -> AnyPlutusScriptVersion
  -> ScriptDataOrFile
  -> ExecutionUnits
  -> CliCertificateScriptRequirements
createPlutusReferenceScriptFromCliArgs :: TxIn
-> AnyPlutusScriptVersion
-> ScriptDataOrFile
-> ExecutionUnits
-> CliCertificateScriptRequirements
createPlutusReferenceScriptFromCliArgs TxIn
txIn AnyPlutusScriptVersion
version ScriptDataOrFile
redeemer ExecutionUnits
execUnits =
  PlutusRefScriptCliArgs -> CliCertificateScriptRequirements
OnDiskPlutusRefScript (PlutusRefScriptCliArgs -> CliCertificateScriptRequirements)
-> PlutusRefScriptCliArgs -> CliCertificateScriptRequirements
forall a b. (a -> b) -> a -> b
$ TxIn
-> AnyPlutusScriptVersion
-> ScriptDataOrFile
-> ExecutionUnits
-> PlutusRefScriptCliArgs
PlutusRefScriptCliArgs TxIn
txIn AnyPlutusScriptVersion
version ScriptDataOrFile
redeemer ExecutionUnits
execUnits