                                   :target :home}
                                  {:event  :goto-betting
                                   :target :betting}]
-                   }}}))+                   }}})
+
+  )
   (let [state-index (StatechartIndex. {})
         state       (make-statechart [] (assoc state :id []
                                                      :state-index state-index)
                                      state-index)]
     (set-index state-index (index-states state))
     state))
  ([path state state-index]
   (-> (reduce-kv (fn [state id substate]
                    (assoc-in state [:states id] (make-statechart (conj path id) substate state-index)))
                  state
                  (:states state))
       (assoc :id path
              :state-index state-index)
       (make-transitions))))

(comment
  (def statechart

                 (butlast)
                 vec))))

(defn parent-state [state]
  (let [{:keys [state-index]} state]
    (get state-index (parent-id state))))

(defn select-event-transitions [state event]
  (concat (->> (state-transitions state)
               (filter #(= (trigger-event %) event)))
          (lazy-seq (if-some [parent (parent-state state)]
                      (select-event-transitions parent event)))))

(defn select-eventless-transitions [state]
  (concat (->> (state-transitions state)
               (filter #(nil? (trigger-event %))))
          (lazy-seq (if-some [parent (parent-state state)]
                      (select-eventless-transitions parent)))))

(defn state-zip [state]
  (zip/zipper
    (constantly true)
    substates
    (fn [state] (prn "edit"))
    state))

(defn make-transitions [state state-index]
  (let [{:keys [id transitions]} state]
    (cond-> state
            (some? transitions)
            (update :transitions
                    (fn [transitions]
                      (->> transitions
                           (mapv (fn [transition]
                                   (let [{:keys [target]} transition]
                                     (cond-> (assoc transition
                                               :source id
                                               :state-index state-index)
                                             (keyword? target)
                                             (assoc :target (conj id target))))))))))))

(defn index-states [state]
  (loop [loc (state-zip state)
