       (.dimensions (clj->js (map name (get-parsets-agg))))
       (.value (fn [d i] (. d -value)))))
 
-(defcomponent parsets [cursor owner {:keys [agg url height] :or {height 600} :as opts}]
+(defcomponent parsets [cursor owner {:keys [agg value-path url height] :or {height 600 value-path ["doc_count"]} :as opts}]
   (init-state [_]
               {:continue? true
                :comm (chan)

            [searchbot.input.labels :as labels]))

(defn- build-parsets-agg
  [terms]
  (reduce (fn [agg-query a-term]
            {:aggs {(keyword a-term) (merge {:terms {:field a-term}} agg-query)}})
          {} terms))

(defn- walk-buckets [de-buck bucket prefix steps]
  (map #(de-buck % de-buck prefix (first steps) (rest steps))
           (get-in bucket [(first steps) :buckets])))

(defn- <-buckets [bucket de-buck prefix current-step steps]
  (if (= 0 (count steps))
    (merge prefix {current-step (:key bucket) :value (:doc_count bucket)})
    (let [prefix (merge prefix {current-step (:key bucket)})]
      (walk-buckets <-buckets bucket prefix steps))))

(defn- agg->parsets [agg-result all-steps]
  (vec (flatten (walk-buckets <-buckets agg-result {} all-steps))))

(defn- new-svg [graph-id w h]
  (-> js/d3 (.select graph-id) (.append "svg") (.attr "width" w) (.attr "height" h)))

(defn- ->query
  [{:keys [agg-terms default-filter]}]
  (let [agg-query (build-parsets-agg (reverse agg-terms))
        agg-query (filtered-agg {:body agg-query
                                 :filter default-filter})]
    agg-query))

(defn- do-parsets-agg
  [owner]
  (let [{:keys [continue? comm url get-agg-terms make-agg-query]} (om/get-state owner)
        {es-settings :es-settings} (om/get-shared owner)]
    (when continue?
      (go (.log js/console "# running parsets aggregation:" (pr-str (get-agg-terms)))
          (let [{agg-result :aggregations} (<! (es-agg (or url (:url-agg (es-settings))) (make-agg-query)))]
            (>! comm (agg->parsets agg-result (get-agg-terms))))))))

(defn- update-agg-terms-with-label
  [owner label-string]

               :parsets-agg (map keyword (:terms agg))
               :get-agg-terms #(:parsets-agg (om/get-state owner))
               :make-agg-query #(->query {:agg-terms ((:get-agg-terms (om/get-state owner)))
                                          :sub-aggs (:sub agg)
                                          :default-filter (get-in (:es-settings (om/get-shared owner)) [:default :filter])})
               :value-path (apply vector (map keyword value-path))
               :svg nil})
  (will-mount [_]
              (do-parsets-agg owner))
