 (ns workflo.macros.screen.bidi
   (:require [bidi.bidi :as bidi]
+            [bidi.router :as br]
             [workflo.macros.screen :as scr]))
 
 (defn route

   parameterizable URL segments (e.g. :user-id) to their values
   in the URL."
  [url]
  (when-let [result (bidi/match-route (routes) url)]
    {:params (:route-params result)
     :screen (scr/resolve-screen (-> result :handler name symbol))}))

(defn path
  "Returns a URL path for the given screen and the given URL

                      :name)]
    (apply (partial bidi/path-for (routes) screen-name)
           params)))

(defn- on-navigate
  [env location]
  (let [{:keys [screen params]} (match-location location)]
    (some-> env :mount-screen (apply [screen params]))))

(defn router
  [{:keys [default-screen
           mount-screen]
    :or   {default-screen 'home}
    :as   env}]
  (br/start-router! (routes)
                    {:on-navigate #(on-navigate env %)
                     :default-location {:handler default-screen}}))
