module Mit.Stanza
  ( Stanza,
    renderStanzas,
    putStanzas,
  )
where

import Data.List qualified as List
import Data.Text.IO qualified as Text
import Data.Text.Lazy.Builder qualified as Text (Builder)
import Mit.Builder qualified as Builder
import Mit.Prelude

type Stanza =
  Maybe Text.Builder

renderStanzas :: [Stanza] -> Stanza
renderStanzas :: [Stanza] -> Stanza
renderStanzas [Stanza]
stanzas =
  case forall a. [Maybe a] -> [a]
catMaybes [Stanza]
stanzas of
    [] -> forall a. Maybe a
Nothing
    [Builder]
stanzas' -> forall a. a -> Maybe a
Just (forall a. Monoid a => [a] -> a
mconcat (forall a. a -> [a] -> [a]
List.intersperse (Builder
Builder.newline forall a. Semigroup a => a -> a -> a
<> Builder
Builder.newline) [Builder]
stanzas'))

putStanzas :: [Stanza] -> IO ()
putStanzas :: [Stanza] -> IO ()
putStanzas [Stanza]
stanzas =
  forall (m :: * -> *) a.
Applicative m =>
Maybe a -> (a -> m ()) -> m ()
whenJust ([Stanza] -> Stanza
renderStanzas [Stanza]
stanzas) \Builder
s ->
    Text -> IO ()
Text.putStr (Builder -> Text
Builder.build (Builder
Builder.newline forall a. Semigroup a => a -> a -> a
<> Builder
s forall a. Semigroup a => a -> a -> a
<> Builder
Builder.newline forall a. Semigroup a => a -> a -> a
<> Builder
Builder.newline))