module Heist.Extra.Splices.List where

import Data.Map.Syntax ((##))
import Heist qualified as H
import Heist.Interpreted qualified as HI

-- | A splice that applies a non-empty list
listSplice :: [a] -> Text -> (a -> H.Splices (HI.Splice Identity)) -> HI.Splice Identity
listSplice :: forall a.
[a] -> Text -> (a -> Splices (Splice Identity)) -> Splice Identity
listSplice [a]
xs Text
childTag a -> Splices (Splice Identity)
childSplice = do
  if forall (t :: Type -> Type) a. Foldable t => t a -> Bool
null [a]
xs
    then forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
    else forall (n :: Type -> Type).
Monad n =>
Splices (Splice n) -> Splice n
HI.runChildrenWith forall a b. (a -> b) -> a -> b
$ do
      Text
childTag
        ## (HI.runChildrenWith . childSplice)
          `foldMapM` xs