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

module Cardano.CLI.EraBased.Script.Spend.Types
  ( CliSpendScriptRequirements (..)
  , PlutusRefScriptCliArgs (..)
  , SimpleOrPlutusScriptCliArgs (..)
  , ScriptDatumOrFileSpending (..)
  , SimpleRefScriptCliArgs (..)
  , SpendScriptWitness (..)
  , createSimpleOrPlutusScriptFromCliArgs
  , createPlutusReferenceScriptFromCliArgs
  , createSimpleReferenceScriptFromCliArgs
  )
where

import           Cardano.Api

import           Cardano.CLI.Types.Common (ScriptDataOrFile)

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

data CliSpendScriptRequirements
  = OnDiskSimpleOrPlutusScript SimpleOrPlutusScriptCliArgs
  | OnDiskSimpleRefScript SimpleRefScriptCliArgs
  | OnDiskPlutusRefScript PlutusRefScriptCliArgs
  deriving Int -> CliSpendScriptRequirements -> ShowS
[CliSpendScriptRequirements] -> ShowS
CliSpendScriptRequirements -> String
(Int -> CliSpendScriptRequirements -> ShowS)
-> (CliSpendScriptRequirements -> String)
-> ([CliSpendScriptRequirements] -> ShowS)
-> Show CliSpendScriptRequirements
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CliSpendScriptRequirements -> ShowS
showsPrec :: Int -> CliSpendScriptRequirements -> ShowS
$cshow :: CliSpendScriptRequirements -> String
show :: CliSpendScriptRequirements -> String
$cshowList :: [CliSpendScriptRequirements] -> ShowS
showList :: [CliSpendScriptRequirements] -> ShowS
Show

data SimpleOrPlutusScriptCliArgs
  = OnDiskPlutusScriptCliArgs
      (File ScriptInAnyLang In)
      ScriptDatumOrFileSpending
      -- ^ Optional Datum (CIP-69)
      ScriptDataOrFile
      -- ^ Redeemer
      ExecutionUnits
  | OnDiskSimpleCliArgs
      (File ScriptInAnyLang In)
  deriving Int -> SimpleOrPlutusScriptCliArgs -> ShowS
[SimpleOrPlutusScriptCliArgs] -> ShowS
SimpleOrPlutusScriptCliArgs -> String
(Int -> SimpleOrPlutusScriptCliArgs -> ShowS)
-> (SimpleOrPlutusScriptCliArgs -> String)
-> ([SimpleOrPlutusScriptCliArgs] -> ShowS)
-> Show SimpleOrPlutusScriptCliArgs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SimpleOrPlutusScriptCliArgs -> ShowS
showsPrec :: Int -> SimpleOrPlutusScriptCliArgs -> ShowS
$cshow :: SimpleOrPlutusScriptCliArgs -> String
show :: SimpleOrPlutusScriptCliArgs -> String
$cshowList :: [SimpleOrPlutusScriptCliArgs] -> ShowS
showList :: [SimpleOrPlutusScriptCliArgs] -> ShowS
Show

createSimpleOrPlutusScriptFromCliArgs
  :: File ScriptInAnyLang In
  -> Maybe (ScriptDatumOrFileSpending, ScriptDataOrFile, ExecutionUnits)
  -> CliSpendScriptRequirements
createSimpleOrPlutusScriptFromCliArgs :: File ScriptInAnyLang 'In
-> Maybe
     (ScriptDatumOrFileSpending, ScriptDataOrFile, ExecutionUnits)
-> CliSpendScriptRequirements
createSimpleOrPlutusScriptFromCliArgs File ScriptInAnyLang 'In
scriptFp (Just (ScriptDatumOrFileSpending
datumFile, ScriptDataOrFile
redeemerFile, ExecutionUnits
execUnits)) =
  SimpleOrPlutusScriptCliArgs -> CliSpendScriptRequirements
OnDiskSimpleOrPlutusScript (SimpleOrPlutusScriptCliArgs -> CliSpendScriptRequirements)
-> SimpleOrPlutusScriptCliArgs -> CliSpendScriptRequirements
forall a b. (a -> b) -> a -> b
$ File ScriptInAnyLang 'In
-> ScriptDatumOrFileSpending
-> ScriptDataOrFile
-> ExecutionUnits
-> SimpleOrPlutusScriptCliArgs
OnDiskPlutusScriptCliArgs File ScriptInAnyLang 'In
scriptFp ScriptDatumOrFileSpending
datumFile ScriptDataOrFile
redeemerFile ExecutionUnits
execUnits
createSimpleOrPlutusScriptFromCliArgs File ScriptInAnyLang 'In
scriptFp Maybe (ScriptDatumOrFileSpending, ScriptDataOrFile, ExecutionUnits)
Nothing = SimpleOrPlutusScriptCliArgs -> CliSpendScriptRequirements
OnDiskSimpleOrPlutusScript (SimpleOrPlutusScriptCliArgs -> CliSpendScriptRequirements)
-> SimpleOrPlutusScriptCliArgs -> CliSpendScriptRequirements
forall a b. (a -> b) -> a -> b
$ File ScriptInAnyLang 'In -> SimpleOrPlutusScriptCliArgs
OnDiskSimpleCliArgs File ScriptInAnyLang 'In
scriptFp

newtype SimpleRefScriptCliArgs = SimpleRefScriptArgs TxIn deriving Int -> SimpleRefScriptCliArgs -> ShowS
[SimpleRefScriptCliArgs] -> ShowS
SimpleRefScriptCliArgs -> String
(Int -> SimpleRefScriptCliArgs -> ShowS)
-> (SimpleRefScriptCliArgs -> String)
-> ([SimpleRefScriptCliArgs] -> ShowS)
-> Show SimpleRefScriptCliArgs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SimpleRefScriptCliArgs -> ShowS
showsPrec :: Int -> SimpleRefScriptCliArgs -> ShowS
$cshow :: SimpleRefScriptCliArgs -> String
show :: SimpleRefScriptCliArgs -> String
$cshowList :: [SimpleRefScriptCliArgs] -> ShowS
showList :: [SimpleRefScriptCliArgs] -> ShowS
Show

createSimpleReferenceScriptFromCliArgs :: TxIn -> CliSpendScriptRequirements
createSimpleReferenceScriptFromCliArgs :: TxIn -> CliSpendScriptRequirements
createSimpleReferenceScriptFromCliArgs = SimpleRefScriptCliArgs -> CliSpendScriptRequirements
OnDiskSimpleRefScript (SimpleRefScriptCliArgs -> CliSpendScriptRequirements)
-> (TxIn -> SimpleRefScriptCliArgs)
-> TxIn
-> CliSpendScriptRequirements
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxIn -> SimpleRefScriptCliArgs
SimpleRefScriptArgs

data PlutusRefScriptCliArgs
  = PlutusRefScriptCliArgs
      TxIn
      -- ^ TxIn with reference script
      AnyPlutusScriptVersion
      ScriptDatumOrFileSpending
      -- ^ Optional Datum (CIP-69)
      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
  -> ScriptDatumOrFileSpending
  -> ScriptDataOrFile
  -> ExecutionUnits
  -> CliSpendScriptRequirements
createPlutusReferenceScriptFromCliArgs :: TxIn
-> AnyPlutusScriptVersion
-> ScriptDatumOrFileSpending
-> ScriptDataOrFile
-> ExecutionUnits
-> CliSpendScriptRequirements
createPlutusReferenceScriptFromCliArgs TxIn
txin AnyPlutusScriptVersion
v ScriptDatumOrFileSpending
mDatum ScriptDataOrFile
redeemer ExecutionUnits
execUnits =
  PlutusRefScriptCliArgs -> CliSpendScriptRequirements
OnDiskPlutusRefScript (PlutusRefScriptCliArgs -> CliSpendScriptRequirements)
-> PlutusRefScriptCliArgs -> CliSpendScriptRequirements
forall a b. (a -> b) -> a -> b
$ TxIn
-> AnyPlutusScriptVersion
-> ScriptDatumOrFileSpending
-> ScriptDataOrFile
-> ExecutionUnits
-> PlutusRefScriptCliArgs
PlutusRefScriptCliArgs TxIn
txin AnyPlutusScriptVersion
v ScriptDatumOrFileSpending
mDatum ScriptDataOrFile
redeemer ExecutionUnits
execUnits

data ScriptDatumOrFileSpending
  = PotentialDatum (Maybe ScriptDataOrFile)
  | InlineDatum
  deriving Int -> ScriptDatumOrFileSpending -> ShowS
[ScriptDatumOrFileSpending] -> ShowS
ScriptDatumOrFileSpending -> String
(Int -> ScriptDatumOrFileSpending -> ShowS)
-> (ScriptDatumOrFileSpending -> String)
-> ([ScriptDatumOrFileSpending] -> ShowS)
-> Show ScriptDatumOrFileSpending
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptDatumOrFileSpending -> ShowS
showsPrec :: Int -> ScriptDatumOrFileSpending -> ShowS
$cshow :: ScriptDatumOrFileSpending -> String
show :: ScriptDatumOrFileSpending -> String
$cshowList :: [ScriptDatumOrFileSpending] -> ShowS
showList :: [ScriptDatumOrFileSpending] -> ShowS
Show