{-# LANGUAGE OverloadedStrings #-}

module PlutusTx.Blueprint.Write (
  encodeBlueprint,
  writeBlueprint,
) where

import Data.Aeson (toJSON)
import Data.Aeson.Encode.Pretty (encodePretty')
import Data.Aeson.Encode.Pretty qualified as Pretty
import Data.ByteString.Lazy qualified as LBS
import PlutusTx.Blueprint.Contract (ContractBlueprint)
import Prelude

writeBlueprint :: FilePath -> ContractBlueprint -> IO ()
writeBlueprint :: FilePath -> ContractBlueprint -> IO ()
writeBlueprint FilePath
f ContractBlueprint
blueprint = FilePath -> ByteString -> IO ()
LBS.writeFile FilePath
f (ContractBlueprint -> ByteString
encodeBlueprint ContractBlueprint
blueprint)

encodeBlueprint :: ContractBlueprint -> LBS.ByteString
encodeBlueprint :: ContractBlueprint -> ByteString
encodeBlueprint =
  Config -> Value -> ByteString
forall a. ToJSON a => Config -> a -> ByteString
encodePretty'
    Config
Pretty.defConfig
      { Pretty.confIndent = Pretty.Spaces 2
      , Pretty.confCompare =
          Pretty.keyOrder
            [ "$id"
            , "$schema"
            , "$vocabulary"
            , "preamble"
            , "validators"
            , "definitions"
            , "title"
            , "description"
            , "version"
            , "plutusVersion"
            , "license"
            , "redeemer"
            , "datum"
            , "parameters"
            , "purpose"
            , "schema"
            ]
      , Pretty.confNumFormat = Pretty.Generic
      , Pretty.confTrailingNewline = True
      }
    (Value -> ByteString)
-> (ContractBlueprint -> Value) -> ContractBlueprint -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractBlueprint -> Value
forall a. ToJSON a => a -> Value
toJSON