 (ns devtools.core
   (:require [devtools.debug :as debug]))
 
-(def max-seq-elements 10)
+(def max-coll-elements 10)
 (def max-map-elements 5)
 (def max-set-elements 7)
-(def abbreviation-string "…")
+(def abbreviation "…")
 (def line-index-separator ":")
 (def formatter-key "devtoolsFormatter")
+(def dq "\"")
 
 (def ^:dynamic *devtools-enabled* true)
 (def ^:dynamic *devtools-installed* false)

(defn something-abbreviated? [value]
  (if (coll? value)
    (some #(something-abbreviated? %) value)
    (= abbreviation-string value)))

(defn abbreviated? [template]
  (something-abbreviated? (js->clj template)))


; TODO: abbreviate long strings
(defn string-template [value]
  (template "span" "color:#C41A16" (str dq value dq)))

(defn fn-template [value]
  (template "span" "color:#f00" (reference value) "fn"))

(defn header-inlined-templates [value renderer max]
  (let [rendered-items (apply concat (interpose [(spacer)] (map renderer (take max value))))]
    (if (> (count value) max)
      (concat rendered-items [abbreviation])
      rendered-items)))

(defn header-coll-template [value]
  (let [renderer (fn [item] [(inlined-value-template item)])
        items (header-inlined-templates value renderer max-coll-elements)]
    (template "span" "color:#000" "[" items "]")))

(defn header-map-template [value]
  (let [renderer (fn [[key value]] [(inlined-value-template key) (spacer) (inlined-value-template value)])
        items (header-inlined-templates value renderer max-map-elements)]
    (template "span" "color:#000" "{" items "}")))

(defn header-set-template [value]
  (let [renderer (fn [item] [(inlined-value-template item)])
        items (header-inlined-templates value renderer max-set-elements)]
    (template "span" "color:#000" "#{" items "}")))

(defn generic-template [value]
  (template "span" "color:#000" (reference value)))
