             [promesa.core :as p])
   (:import [goog.date DateTime]))
 
-(declare client)
-(enable-console-print!)
+;; Instanciate the postal client
+(def client (pc/client "http://localhost:5050/api"))
 
 (defn- timestamp
+  "Return a iso string formated the current time."
   []
   (let [d (DateTime.)]
     (.toIsoString d)))
 
 (defn- build-entry
+  "Build chat entry dom node."
   [author text]
   (let [author (if (empty? author)
                  "Anonymous"

                   (dom/createDom "span" #js {"class" "text"} text))))

(defn- append-message
  [{:keys [data]}]
  (let [entry (build-entry (:author data)
                           (:text data))

    (dom/appendChild el entry)
    (style/scrollIntoContainerView entry el)))

(defn main
  []
  (enable-console-print!)

  (letfn [(on-key-up [event]
            (when (= 13 (.-keyCode event))
              (let [author-node (dom/getElement "author-input")
                    author (.-value author-node)
                    target (.-target event)
                    text (.-value target)
                    data {:author author
                          :text text}]
                ;; Send data to the backend
                (pc/novelty client :chats data)

                ;; Empty the text input
                (set! (.-value target) ""))))]

    (let [input (dom/getElement "message-input")
          bus (pc/subscribe client :chats)]
      (events/listen input "keyup" on-key-up)
      (s/on-value bus append-message))))

(main)
