{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
module Patat.Eval.Internal
    ( EvalBlocks
    , EvalBlock (..)
    , renderEvalBlock
    ) where


--------------------------------------------------------------------------------
import qualified Control.Concurrent.Async       as Async
import qualified Data.HashMap.Strict            as HMS
import qualified Data.Text                      as T
import           Patat.Presentation.Instruction
import           Patat.Presentation.Settings
import qualified Text.Pandoc                    as Pandoc


--------------------------------------------------------------------------------
type EvalBlocks = HMS.HashMap Var EvalBlock


--------------------------------------------------------------------------------
-- | Block that needs to be evaluated.
data EvalBlock = EvalBlock
    { EvalBlock -> EvalSettings
ebSettings :: !EvalSettings
    , EvalBlock -> Attr
ebAttr     :: !Pandoc.Attr
    , EvalBlock -> Text
ebInput    :: !T.Text
    , EvalBlock -> Maybe (Async ())
ebAsync    :: !(Maybe (Async.Async ()))
    }


--------------------------------------------------------------------------------
renderEvalBlock :: EvalBlock -> T.Text -> [Pandoc.Block]
renderEvalBlock :: EvalBlock -> Text -> [Block]
renderEvalBlock EvalBlock {Maybe (Async ())
Attr
Text
EvalSettings
ebSettings :: EvalBlock -> EvalSettings
ebAttr :: EvalBlock -> Attr
ebInput :: EvalBlock -> Text
ebAsync :: EvalBlock -> Maybe (Async ())
ebSettings :: EvalSettings
ebAttr :: Attr
ebInput :: Text
ebAsync :: Maybe (Async ())
..} Text
out = case EvalSettings -> EvalSettingsContainer
evalContainer EvalSettings
ebSettings of
    EvalSettingsContainer
EvalContainerCode   -> [Attr -> Text -> Block
Pandoc.CodeBlock Attr
ebAttr Text
out]
    EvalSettingsContainer
EvalContainerNone   -> [Format -> Text -> Block
Pandoc.RawBlock Format
fmt Text
out]
    EvalSettingsContainer
EvalContainerInline -> [[Inline] -> Block
Pandoc.Plain [Format -> Text -> Inline
Pandoc.RawInline Format
fmt Text
out]]
  where
    fmt :: Format
fmt = Format
"eval"