                          :js-file "js/main.js"
                          :js-dir "js/out"
                          :main-div "main-content"
-                         :default-title ""
-                         :allow-html5-history false}))
-
-;; (defonce page (r/atom "index.html"))
-;; (defonce page-state (r/atom {:has-history false}))
+                         :default-title ""}))
 
 (defonce history nil)
-
-(defn as-relative [f]
-  (clojure.string/replace f #"^/" ""))
 
 (defn demo-handler [state [id v1 v2 :as event]]
   (case id
     :content (do
                (let [title (if v2
-                             (str (:title-prefix @config) v2)
-                             (str (:default-title @config)))]
+                             (str (:title-prefix state) v2)
+                             (str (:default-title state)))]
                  (when r/is-client
                    (r/next-tick #(set! js/document.title title)))
                  (assoc state

                 (when r/is-client
                   (.setToken history v1 false)
                   (r/next-tick #(set! js/document.body.scrollTop 0)))
                 (recur state [:set-page v1 v2]))
    state))

(defn dispatch [event]
  (dbg event)
  (rswap! config demo-handler event)
  nil)

(defn reg-page [url]
  (swap! config update-in [:pages] conj url))

(defn init-history []
  (when-not history
    (let [page (:page-name @config)
          html5 (let [proto js/location.protocol]
                  (and page
                       (.isSupported Html5History)
                       (#{"http:" "https:"} proto)))]
      (doto (set! history
                  (if html5
                    (doto (Html5History.)
                      (.setUseFragment false)
                      (.setPathPrefix js/location.origin)
                      (.setEnabled true))
                    (History.)))
        (evt/listen hevt/NAVIGATE #(dispatch [:set-page (str (.-token %))]))
        (.setEnabled true))
      (when html5
        (.setToken history page)))))


;; (defn register-page
;;   ([pageurl comp]
;;    (register-page pageurl comp nil))
;;   ([pageurl comp title]
;;    (assert (string? pageurl)
;;            (str "expected string, not " pageurl))
;;    (assert (vector? comp)
;;            (str "expected vector, not " (pr-str comp)))
;;    (assert (or (nil? title)
;;                (string? title)))
;;    (swap! config update-in [:page-map] assoc pageurl comp)
;;    (swap! config update-in [:page-titles] assoc pageurl title)
;;    pageurl))


;;; Components

(defn link [props child]
  [:a (assoc props
             :on-click #(do (.preventDefault %)
                            (dispatch [:goto-page (:href props)])))
   child])

;; (defn link
;;     [props child]
;;     (let [p (:href props)
;;           f ((:page-map @config) p)]
;;       (assert (vector? f) (str "couldn't resolve page " p))
;;       (assert (string? p))
;;       [:a (assoc props
;;                  :href p
;;                  :on-click (if (:has-history @page-state)
;;                              (fn [e]
;;                                (.preventDefault e)
;;                                (reset! page p)
;;                                (r/next-tick
;;                                 #(set! (.-scrollTop (.-body js/document))
;;                                        0)))
;;                              identity))
;;        child]))

(defn page-content []
  (:main-content @config))



;;; Implementation:

;; (defn get-title []
;;   (get-in @config [:page-titles @page]
;;           (or (get-in @config [:page-titles "index.html"])
;;               "")))

;; (defn default-content []
;;   [:div "Empty"])

;; (add-watch page ::title-watch
;;            (fn [_ _ _ p]
;;              (when r/is-client
;;                (set! (.-title js/document) (get-title)))
;;              ;; First title on a page wins
;;              #_(reset! page-title "")))

;;; History

;; (defn use-html5-history []
;;   (when r/is-client
;;     (let [proto (.' js/window :location.protocol)]
;;       (and (:allow-html5-history @config)
;;            (.isSupported Html5History)
;;            (#{"http:" "https:"} proto)))))

;; (defn create-history [p]
;;   (if (use-html5-history)
;;     (doto (Html5History.)
;;       (.setUseFragment false))
;;     (let [h (History.)]
;;       (when p
;;         (.setToken h p))
;;       h)))

;; (defn token-base []
;;   (if (use-html5-history)
;;     (:base-path @config)))

;; (defn setup-history [p]
;;   (when (nil? history)
;;     (set! history (create-history p))
;;     (swap! page-state assoc :has-history (some? history))
;;     (when-let [h history]
;;       (evt/listen h hevt/NAVIGATE
;;                   (fn [e]
;;                     (let [t (.-token e)
;;                           tb (token-base)]
;;                       (reset! page (if (and tb (== 0 (.indexOf t tb)))
;;                                      (subs t (count tb))
;;                                      t)))
;;                     (r/flush)))
;;       (add-watch page ::history
;;                  (fn [_ _ oldp newp]
;;                    (when-not (= oldp newp)
;;                      (.setToken h (str (token-base) newp)))))
;;       (.setEnabled h true))))

;; (defn base-path [loc p]
;;   ;; Find base-path for html5 history
;;   (let [split #".[^/]*"
;;         depth (->> (case p "" "." p) (re-seq split) count)
;;         base (->> loc (re-seq split) (drop-last depth) (apply str))]
;;     (string/replace (str base "/") #"^/" "")))

;; (defn set-start-page [p]
;;   (when (and (not (:base-path @config))
;;              (use-html5-history))
;;     (swap! config assoc :base-path
;;            (base-path (.' js/window -location.pathname) p)))
;;   (reset! page p))

(defn prefix [href page]
  (let [depth (-> #"/" (re-seq page) count)]
    (str (->> "../" (repeat depth) (apply str)) href)))


;;; Static site generation

(defn body []
  (let [b (:body @config)]

        items (as-> f _
                (.' path dirname _)
                (.' path normalize _)
                (string/split _ #"/"))]
    (doseq [d (reductions #(str %1 "/" %2) items)]
      (when-not (.' fs existsSync d)
        (.' fs mkdirSync d)))))

