 (defn plugin-info [dir]
   (or (plugin-json dir) (plugin-edn dir)))
 
+(defn missing-deps [all]
+  (let [deps (->> (vals all)
+                  (mapcat (comp seq :dependencies)))]
+    (-> (reduce (fn [final [name version]]
+                  (let [name (cljs.core/name name)]
+                    (if-let [cur (or (get all name) (get final name))]
+                      ;;check if it's newer
+                      (if (deploy/is-newer? (:version cur) version)
+                        (assoc! final name {:name name
+                                            :version version})
+                        final)
+                      (assoc! final name {:name name
+                                          :version version}))))
+                (transient {})
+                deps)
+        (persistent!)
+        (vals)
+        (seq))))
+
+(defn install-missing [missing]
+  (let [counter (atom (count missing))
+        count-down (fn []
+                     (swap! counter dec)
+                     ;;then install the actual plugin
+                     (when (<= @counter 0)
+                       (cmd/exec! :behaviors.reload)
+                       (object/raise manager :refresh!)
+                       (notifos/set-msg! "All missing dependencies installed.")
+                       ))]
+    ;;first get and install all the deps
+    ;;count them down and then install the real plugin and reload.
+    (doseq [dep missing]
+      (discover-deps dep count-down))))
+
 (defn available-plugins []
   (let [ds (concat (files/dirs user-plugins-dir)
                    (files/dirs plugins-dir))
         plugins (->> ds
                      (map plugin-info)
-                     (filterv identity))]
-    (-> (reduce (fn [final p]
-                  (if-let [cur (get final (:name p))]
-                    ;;check if it's newer
-                    (if (deploy/is-newer? (:version cur) (:version p))
-                      (assoc! final (:name p) p)
-                      final)
-                    (assoc! final (:name p) p)))
-                (transient {})
-                plugins)
-        (persistent!))))
+                     (filterv identity))
+        final (-> (reduce (fn [final p]
+                            (if-let [cur (get final (:name p))]
+                              ;;check if it's newer
+                              (if (deploy/is-newer? (:version cur) (:version p))
+                                (assoc! final (:name p) p)
+                                final)
+                              (assoc! final (:name p) p)))
+                          (transient {})
+                          plugins)
+                  (persistent!))
+        missing? (missing-deps final)]
+    (when missing?
+      (popup/popup! {:header "Some plugin dependencies are missing."
+                     :body [:div
+                            [:span "We found that the following plugin dependencies are missing: "]
+                             (for [{:keys [name version]} missing?]
+                               [:div name " " version " "])
+                            [:span "Would you like us to install them?"]]
+                     :buttons [{:label "Cancel"}
+                               {:label "Install all"
+                                :action (fn []
+                                          (install-missing missing?))}]}))
+    final))
 
 
 (defn plugin-behaviors [plug]

           (this-as me
                    (discover-deps plugin (fn []
                                            (dom/remove (dom/parent me))
                                            )))
           (dom/prevent e)
           (dom/stop-propagation e)))

            [lt.util.kahn :as kahn]
            [lt.util.load :as load]
            [lt.util.dom :as dom]
            [clojure.set :as set]
            [clojure.string :as string]
            [clojure.walk :as walk])
  (:require-macros [lt.macros :refer [behavior defui]]))
