       (mod 2)
       (= 1)))
 
-(defn insert [boxes box new-box direction]
+(defn insert [boxes direction box new-box]
   (loop [loc (zip/seq-zip boxes)]
+    (print (zip/node loc))
     (if (zip/end? loc)
-      (zip/root loc)
+      (do
+        (print "end")
+        (zip/root loc))
       (if (= box (zip/node loc))
         (zip/root
           (case direction

(ns fugue.space
  (:require [clojure.zip :as zip]
            [reagent.core :as r]))

(def init-state
  {:boxes [1 [2 3]]
   :active-box 1
   :next-id 4
   :files {1 "foo" 2 "bar" 3 "biz"}})

(defn in-row? [loc]
  (-> (zip/path loc)

            :left (if (in-row? loc)
                    (zip/insert-left loc new-box)
                    (zip/edit loc #(list new-box %)))
            :below (if (in-row? loc)
                     (zip/edit loc #(list % new-box))
                     (zip/insert-right loc new-box))
            :above (if (in-row? loc)
                     (zip/edit loc #(list new-box %))
                     (zip/insert-left loc new-box))))
        (recur (zip/next loc))))))


(defn init-state [state]
  (assoc-in state [:cljs.analyzer/namespaces 'cljs.user]
            (analyzer-state 'fugue.api)))

(def state (cljs.js/empty-state init-state))

(def eval-settings
  {:eval cljs.js/js-eval
   :context :statement})

(defn eval-str [source load-fn cb]
  (cljs.js/eval-str state source nil (assoc eval-settings :load load-fn) cb))

(defn do-eval [state cb]
  (let [text (get-in state [:files (:active-box state)])
        load-fn (fn [m cb]
                  (if-let [source (get-in state [:files (str (:name m))])]
                    (cb {:lang :clj :source source})
                    (cb nil)))]
    (eval-str text load-fn cb)))

(defn boxes [files box is-row]
  [:div {:style {:display "flex"
                 :flex-grow 0
                 :flex-flow (if is-row "row" "column")}}
   (if (seqable? box)
     (map #(boxes files % (not is-row)) box)
     [:p (str (get files box))])])

(defn app []
  (let [state (r/atom init-state-m)]
    (fn []
      [:div
       [:button {:on-click #(do-eval @state (partial swap! state update :boxes insert :below (:active-box @state)))}
        "Click me"]
       [:p (str @state)]
       (boxes (:files @state) (:boxes @state) true)])))



