   (will-mount [_]
     (let [{:keys [pick-channel]} (om/get-shared owner)]
       (go-loop []
-        (when-let [{:keys [selected-field-id]} (<! pick-channel)]
-          (put! (om/get-state owner :slave-fields-picker-chan)
-                {:selected-master-field-id selected-field-id})
-          (om/set-state! owner :selected-field-id selected-field-id))
+        (let [{:keys [selected-field-id selected-value
+                      selected-slave-fields]} (<! pick-channel)]
+          (when selected-field-id
+            (put! (om/get-state owner :slave-fields-picker-chan)
+                  {:selected-master-field-id selected-field-id})
+            (om/set-state! owner :selected-field-id selected-field-id))
+          (when selected-value
+            (om/set-state! owner :selected-value selected-value)
+            (om/set-state! owner :selected-slave-fields #{})
+            (put! (om/get-state owner :slave-fields-picker-chan)
+                  {:wipe-selected-slave-fields true}))
+          (when selected-slave-fields
+            (om/set-state! owner :selected-slave-fields selected-slave-fields)))
+        (let [{:keys [selected-field-id
+                      selected-value
+                      selected-slave-fields] :as state} (om/get-state owner)]
+          (when (and selected-field-id selected-value)
+            ; when a conditions values have just changed
+            (when (not (empty? selected-slave-fields))
+              (om/transact! app-state
+                            :conditions
+                            (fn [conditions]
+                              (let [conditions (remove-condition selected-field-id
+                                                                 selected-value
+                                                                 conditions)]
+                                (conj conditions {:master-field-id selected-field-id
+                                                  :value           selected-value
+                                                  :slave-fields    selected-slave-fields})))))
+            (when (empty? selected-slave-fields)
+              ; when all values have been toggled off for a condition
+              (om/transact! app-state
+                            :conditions
+                            (partial remove-condition
+                                     selected-field-id
+                                     selected-value)))))
         (recur))))
-  (render-state [_ {:keys [selected-field-id slave-fields-picker-chan]}]
+  (render-state [_ {:keys [selected-field-id slave-fields-picker-chan
+                           selected-value]}]
     (dom/div
       (om/build master-field-picker (:ticket-fields app-state) {:opts selected-field-id})
       (let [selected-field (first (filter #(= selected-field-id (:id %))

        (om/build value-picker
                  possible-values))
      (om/build slave-fields-picker
                (:ticket-fields app-state)
                {:opts {:slave-fields-picker-chan slave-fields-picker-chan}}))))

(defn main []
  (om/root

                      {:name "High" :value "high"}
                      {:name "Urgent" :value "urgent"}]}

   {:name            "custom field"
    :id              4321
    :type            :tagger
    :possible-values [{:name "asdfasd" :value "asdfasd"}
                      {:name "12345" :value "12345"}]}
   {:name            "a third field"
    :id              9999
    :type            :tagger
    :possible-values [{:name "value number one" :value "v1"}
                      {:name "value number two" :value "v2"}]}])

(defonce app-state (atom {:ticket-fields dummy-ticket-fields
                          :conditions    #{}}))

(declare render-state)
(declare init-state)
(declare will-mount)

(defcomponent conditions-manager [conditions owner]
  (render-state [_ state]
    (dom/div
      (dom/h2
        "conditions")
      (pr-str conditions))))



(defcomponent master-field-picker [ticket-fields owner]
  (render-state [_ {:keys [selected-field-id]}]
