                  (butlast)
                  vec))))
 
-(defn parent [state]
+(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]
-  (let [{:keys [id]} state]
-    (update state :transitions
-            (fn [transitions]
-              (->> transitions
-                   (map (fn [transition]
-                          (let [{:keys [target]} transition]
-                            (cond-> transition
-                              (keyword? target)
-                              (assoc :target (conj id target)))))))))))
-
-
+    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)

(ns sc-demo-page.statechart1
  (:require [clojure.zip :as zip]))

(defprotocol State
  (initialize-state [this])
  (substates [this])
  (state-id [this]))

(extend-protocol State
  clojure.lang.PersistentArrayMap
  (initialize-state [this]
    (case (:type this)
      :and
      (mapcat initialize-state (vals (:states this)))

      :xor
      [(get-in this [:states (:init this)])]

      [this]))
  (substates [this]
    (vals (:states this)))
  (state-id [this]
    (:id this)))

(defn parent-id [state]
  (let [id (state-id state)]

                                  :target :home}
                                 {:event  :goto-betting
                                  :target :betting}]
                   }}})

  )