 (ns via.subs
   (:require [via.events :refer [reg-event-via]]
             [via.endpoint :as via]
+            [utilis.types.keyword :refer [->keyword]]
             [re-frame.core :refer [reg-sub-raw reg-event-db dispatch] :as re-frame]
             [reagent.ratom :refer [make-reaction]]
             [integrant.core :as ig]


(defn- path
  [query-v]
  [:via/subs :cache query-v])

(defn- via-subscribe
  [endpoint query-v]

  (via/dispose endpoint sub-key))

(defn subscribe
  [query-v]
  (let [[local-query-id & _ :as local-query-v]
        (update query-v 0 #(->> % str rest (apply str "via.") ->keyword))]
    (if (re-frame.registrar/get-handler :sub local-query-id)
      (re-frame/subscribe local-query-v)
      (do
        (reg-sub-raw
         local-query-id
         (fn [db local-query-v]
           (swap! subscriptions conj query-v)
           (make-reaction
            #(get-in @db (path query-v))
            :on-dispose #(swap! subscriptions disj query-v))))
        (re-frame/subscribe local-query-v)))))

(reg-event-via
 :via.subs.db/updated
