 (defn update-results-state [in]
   (go-loop []
     (.log js/console (swap! state assoc :results (<! in)))))
-
-; (defn timeout [ms]
-;   (let [c (chan)]
-;     (js/setTimeout (fn [] (close! c)) ms)
-;     c))
 
 (defn throttle [c ms]
   (let [c' (chan)]

(defn search-field []
  (dom/getElement "search-field"))

; Capture keypresses on search field and perform searches as user types,
; throttled to an interval
(defn query-input-loop []
  (let [typing (listen (search-field) "keypress")]
    (go-loop []

      (>! query-chan (.-value (search-field))))))

; Templating
(def lookup-label
  {:category-drugs "Therapeutic Category: Drug(s)"
   :organ-system "Organ System"
   :rationale "Rationale"
   :recommendation "Recommendation"
   :quality-of-evidence "Quality Of Evidence"
   :strength-of-recommendation "Strength of Recommendation"
   :evidence "Evidence"})

(defsnippet result-data "templates/results.html" [:.list-row] [[key val]]
  {[:.list-row] (kioo/content [:li (get key lookup-label)]
                              [:li val])})

(defsnippet result-card "templates/results.html" [:.card] [result-map]
  {[:.list-column] (kioo/content [:li
                                   [:ul {:class "list-row"}
                                     (map result-data
                                       result-map)]])})

(deftemplate result-cards "templates/results.html" []
  {[:.card] (kioo/content (map result-card (get-in @state [:results :_source])}))})

(deftemplate page "index.html" []
  {[:.results] (kioo/content (result-cards))})

(defn init []
  (reagent/render-component [page] (.-body js/document))
  (query-input-loop))
