-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

module Morley.Michelson.Printer
  ( RenderDoc(..)
  , printUntypedContract
  , printTypedContractCode
  , printTypedContract
  , printSomeContract
  , printTypedValue
  , printUntypedValue
  ) where

import Data.Singletons (SingI)
import Data.Text.Lazy qualified as TL

import Morley.Michelson.Printer.Util
import Morley.Michelson.Typed qualified as T
import Morley.Michelson.Untyped qualified as U

-- | Convert an untyped contract into a textual representation which
-- will be accepted by the OCaml reference client: @octez-client@.
printUntypedContract :: (RenderDoc op) => Bool -> U.Contract' op -> TL.Text
printUntypedContract :: forall op. RenderDoc op => Bool -> Contract' op -> Text
printUntypedContract = Bool -> Contract' op -> Text
forall a b. (RenderDoc a, FromSimpleDoc b) => Bool -> a -> b
printRenderDoc

-- | Convert a typed contract into a textual representation which
-- will be accepted by the OCaml reference client: @octez-client@.
printTypedContractCode :: (SingI p, SingI s) => Bool -> T.ContractCode p s -> TL.Text
printTypedContractCode :: forall (p :: T) (s :: T).
(SingI p, SingI s) =>
Bool -> ContractCode p s -> Text
printTypedContractCode Bool
forceSingleLine =
  Bool -> Contract' ExpandedOp -> Text
forall op. RenderDoc op => Bool -> Contract' op -> Text
printUntypedContract Bool
forceSingleLine (Contract' ExpandedOp -> Text)
-> (ContractCode p s -> Contract' ExpandedOp)
-> ContractCode p s
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractCode p s -> Contract' ExpandedOp
forall (param :: T) (store :: T).
(SingI param, SingI store) =>
ContractCode param store -> Contract' ExpandedOp
T.convertContractCode

-- | Convert typed contract into a textual representation which
-- will be accepted by the OCaml reference client: @octez-client@.
printTypedContract :: Bool -> T.Contract p s -> TL.Text
printTypedContract :: forall (p :: T) (s :: T). Bool -> Contract p s -> Text
printTypedContract Bool
forceSingleLine fc :: Contract p s
fc@T.Contract{} =
  Bool -> Contract' ExpandedOp -> Text
forall op. RenderDoc op => Bool -> Contract' op -> Text
printUntypedContract Bool
forceSingleLine (Contract' ExpandedOp -> Text) -> Contract' ExpandedOp -> Text
forall a b. (a -> b) -> a -> b
$ Contract p s -> Contract' ExpandedOp
forall (param :: T) (store :: T).
Contract param store -> Contract' ExpandedOp
T.convertContract Contract p s
fc

-- | Convert typed value into a textual representation which
-- will be accepted by the OCaml reference client: @octez-client@.
printTypedValue
  :: forall t. (T.UntypedValScope t)
  => Bool -> T.Value t -> TL.Text
printTypedValue :: forall (t :: T). UntypedValScope t => Bool -> Value t -> Text
printTypedValue Bool
forceSingleLine =
  Bool -> Value' [] ExpandedOp -> Text
forall (f :: * -> *) op.
(Foldable f, RenderDoc op) =>
Bool -> Value' f op -> Text
printUntypedValue Bool
forceSingleLine (Value' [] ExpandedOp -> Text)
-> (Value t -> Value' [] ExpandedOp) -> Value t -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value t -> Value' [] ExpandedOp
forall (t :: T).
ForbidOp t =>
Value' Instr t -> Value' [] ExpandedOp
T.untypeValue

-- | Convert untyped value into a textual representation which
-- will be accepted by the OCaml reference client: @octez-client@.
printUntypedValue :: (Foldable f, RenderDoc op) => Bool -> U.Value' f op -> TL.Text
printUntypedValue :: forall (f :: * -> *) op.
(Foldable f, RenderDoc op) =>
Bool -> Value' f op -> Text
printUntypedValue = Bool -> Value' f op -> Text
forall a b. (RenderDoc a, FromSimpleDoc b) => Bool -> a -> b
printRenderDoc

-- | Convert 'T.SomeContract' into a textual representation which
-- will be accepted by the OCaml reference client: @octez-client@.
printSomeContract :: Bool -> T.SomeContract -> TL.Text
printSomeContract :: Bool -> SomeContract -> Text
printSomeContract Bool
forceSingleLine (T.SomeContract Contract cp st
fc) =
  Bool -> Contract cp st -> Text
forall (p :: T) (s :: T). Bool -> Contract p s -> Text
printTypedContract Bool
forceSingleLine Contract cp st
fc