module Byg.Functions.Template
  ( Template(..)
  , makeTemplate
  , applyTemplate
  ) where

import Byg.Types (Token)
import Byg.DependencyGenerator (DepGenM, TokenableTo(..), onTupleToken)

import Data.Text (Text)
import qualified Data.Text as T

data Template = TemplateParts Text Text
  deriving (Eq, Show)

makeTemplate :: (TokenableTo Text a, TokenableTo Text b) => a -> b -> DepGenM (Token Template)
makeTemplate = onTupleToken $ \t c ->
  let (beforeContent, after) = T.breakOn c t
      afterContent = T.drop (T.length c) after
  in TemplateParts beforeContent afterContent

applyTemplate :: (TokenableTo Template a, TokenableTo Text b) => a -> b -> DepGenM (Token Text)
applyTemplate = onTupleToken $ \(TemplateParts beforeContent afterContent) t ->
  T.concat [beforeContent, t, afterContent]
