(include "./colors.sibilant")

(def sibilant.pretty-print (node color entry)
     (default entry true
              color true)

     (real-newlines (if (node? node) (do
                       (var pretty-printer (or (get sibilant.pretty-print node.type)
                                               sibilant.pretty-print.default))
                       (pretty-printer node color entry))
         (list? node)
         (concat (if color (black "[") "")
                 (pipe node
                       (map (#-> (prettify color false)))
                       (join (if color (black ",") "")))
                 (if color (black "]") ""))

           color (red (inspect node))
           (real-newlines (inspect node)))))

(var prettify sibilant.pretty-print)

(def sibilant.pretty-print.default (node color entry)
     (def map-pretty (attr)
          (var arr (get node attr))
          (if (and arr arr.length)
              (|> arr (map (#-> (prettify color false))) join)
              ""))

     (real-newlines (sibilant.pretty-print.colorize node color
      (concat
       (if entry "" (map-pretty 'preceding-ignored))
       (map-pretty 'modifiers)
       node.token
       (map-pretty 'contents)
       (map-pretty 'closing-ignored)
       (or (and node.closed (get acceptable-pairs node.token)) "")))))

(def sibilant.pretty-print.root (node color entry)
     (pipe node.contents
           (map (#-> (prettify color false)))
           (join "\n")))

(def sibilant.pretty-print.output (node color)
     (concat (if color (black "{") "")
             (if (list? node.contents)
                 (pipe node.contents
                       (map (#> (sibilant.pretty-print.colorize node color (prettify #0 color false))))
                       (join (if color (black ",") "")))

                 (sibilant.pretty-print.colorize node color node.contents))
             (if color (black "}") "")))

(def real-newlines (node)
     (|> node
         (.split "\\n")
         (.join "\n")))

(def sibilant.pretty-print.colorize (node color string)
     (if (not color) string
         (= node.hint 'macro) (yellow string)
         (node? node 'output) (purple string)
         (green string)))

