module Text.Madlibs.Cata.Display (displayTree) where
import Control.Recursion
import Data.Tree
import Text.Madlibs.Internal.Types
displayTree :: RandTok -> String
displayTree = drawTree . tokToTree 1.0 . cleanTree
cleanTree :: RandTok -> RandTok
cleanTree = cata algebra
where algebra (ValueF t) = Value t
algebra (ListF [(1, t)]) = t
algebra (ListF x) = List x
tokToTree :: Prob -> RandTok -> Tree String
tokToTree p (Value a) = Node ((take 4 . show . min 1.0) p ++ " " ++ show a) []
tokToTree p (List [(_,Value a)]) = Node ((take 4 . show . min 1.0) p ++ " " ++ show a) []
tokToTree p (List xs) = Node (take 4 . show . min 1.0 $ p) (uncurry tokToTree <$> xs)