 ;;   2.  `events.cljs` for the registration of :initialise-db handler
 ;;
 
-(def default-db           ;; what gets put into app-db by default.
-  {:todos   (sorted-map)  ;; an empty list of todos. Use the (int) :id as the key
-   :showing :all})        ;; show all todos
+(def default-db                                             ;; what gets put into app-db by default.
+  {:todos   (sorted-map)                                    ;; an empty list of todos. Use the (int) :id as the key
+   :showing :all})                                          ;; show all todos
 
 
 ;; -- Local Storage  ----------------------------------------------------------

(s/def ::todo (s/keys :req-un [::id ::title ::done]))
(s/def ::todos (s/and                                       ;; should use the :kind kw to s/map-of (not supported yet)
                 (s/map-of ::id ::todo)                     ;; in this map, each todo is keyed by its :id
                 #(instance? PersistentTreeMap %)           ;; is a sorted-map (not just a map)
                 ))
(s/def ::showing                                            ;; what todos are shown to the user?
  #{:all                                                    ;; all todos are shown
    :active                                                 ;; only todos whose :done is false
    :done                                                   ;; only todos whose :done is true
    })
(s/def ::db (s/keys :req-un [::todos ::showing]))

;; -- Default app-db Value  ---------------------------------------------------

(re-frame/reg-cofx
  :local-store-todos
  (fn [cofx _]
    ;; put the localstore todos into the coeffect under :local-store-todos
    (assoc cofx :local-store-todos
              ;; read in todos from localstore, and process into a sorted map
              (into (sorted-map
                      (some->> (.getItem js/localStorage ls-key)
                               (cljs.reader/read-string))))))) ;; EDN map -> map

