module Language.Ltml.Tree.Parser.AppendixSection
    ( appendixSectionTP
    )
where

import Language.Lsd.AST.SimpleRegex (Star (Star))
import Language.Lsd.AST.Type (NamedType)
import Language.Lsd.AST.Type.AppendixSection
    ( AppendixSectionType (AppendixSectionType)
    )
import Language.Ltml.AST.AppendixSection (AppendixSection (AppendixSection))
import Language.Ltml.Common (Flagged')
import Language.Ltml.Tree (FlaggedInputTree', InputTree', Tree (Leaf, Tree))
import Language.Ltml.Tree.Parser
    ( TreeParser
    , disjNFlaggedTreePF
    , nFlaggedTreePF
    )
import Language.Ltml.Tree.Parser.Document (documentTXP')

appendixSectionTP
    :: NamedType AppendixSectionType
    -> FlaggedInputTree'
    -> TreeParser (Flagged' AppendixSection)
appendixSectionTP :: NamedType AppendixSectionType
-> FlaggedInputTree' -> TreeParser (Flagged' AppendixSection)
appendixSectionTP = (AppendixSectionType
 -> Tree Bool (Maybe Text) Text -> TreeParser AppendixSection)
-> NamedType AppendixSectionType
-> FlaggedInputTree'
-> TreeParser (Flagged' AppendixSection)
forall (m :: * -> *) t flag a b c.
(MonadTreeParser m, RawProperNodeKind t) =>
(t -> Tree flag a b -> m c)
-> NamedType t -> FlaggedTree flag a b -> m (Flagged flag c)
nFlaggedTreePF AppendixSectionType
-> Tree Bool (Maybe Text) Text -> TreeParser AppendixSection
aux
  where
    aux :: AppendixSectionType -> InputTree' -> TreeParser AppendixSection
    aux :: AppendixSectionType
-> Tree Bool (Maybe Text) Text -> TreeParser AppendixSection
aux AppendixSectionType
_ (Leaf Text
_) = String -> TreeParser AppendixSection
forall a. String -> TreeParser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Appendix section node is leaf"
    aux AppendixSectionType
_ (Tree (Just Text
_) [FlaggedInputTree']
_) = String -> TreeParser AppendixSection
forall a. String -> TreeParser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Appendix section node has header"
    aux (AppendixSectionType AppendixSectionFormat
fmt (Star Disjunction (NamedType DocumentType)
t)) (Tree Maybe Text
Nothing [FlaggedInputTree']
trees) =
        AppendixSectionFormat
-> [Flagged' (Node Document)] -> AppendixSection
AppendixSection AppendixSectionFormat
fmt ([Flagged' (Node Document)] -> AppendixSection)
-> TreeParser [Flagged' (Node Document)]
-> TreeParser AppendixSection
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FlaggedInputTree' -> TreeParser (Flagged' (Node Document)))
-> [FlaggedInputTree'] -> TreeParser [Flagged' (Node Document)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((DocumentType
 -> Tree Bool (Maybe Text) Text -> TreeParser (Node Document))
-> Disjunction (NamedType DocumentType)
-> FlaggedInputTree'
-> TreeParser (Flagged' (Node Document))
forall (m :: * -> *) t flag a b c.
(MonadTreeParser m, RawProperNodeKind t) =>
(t -> Tree flag a b -> m c)
-> Disjunction (NamedType t)
-> FlaggedTree flag a b
-> m (Flagged flag c)
disjNFlaggedTreePF DocumentType
-> Tree Bool (Maybe Text) Text -> TreeParser (Node Document)
forall (f :: * -> *).
(Pure f, HangingTextP f) =>
DocumentType
-> Tree Bool (Maybe Text) Text -> TreeParser (f Document)
documentTXP' Disjunction (NamedType DocumentType)
t) [FlaggedInputTree']
trees