             [lt.objs.deploy :as deploy]
             [lt.util.dom :as dom]
             [lt.util.load :as load]
-            [crate.binding :refer [bound -value subatom]])
+            [crate.binding :refer [bound -value subatom]]
+            [crate.compiler :refer [dom-attr]])
   (:require-macros [lt.macros :refer [behavior defui]]))
 
 (defn css-expr [k v]

                  :type :user
                  :exclusive true
                  :reaction (fn [this sel]
                              (load-theme sel)
                              (editor/set-options this {:theme sel})))


(def prev-theme "")

(defn load-theme [theme]
  (let [themes (object/raise-reduce app/app :themes+ {})
        path (get themes theme (plugins/adjust-path "core/css/themes/default.css"))]
    (let [elem (load/css path)]
      (dom-attr elem {:id (str "theme-" theme)})
      elem)))

(defn inject-theme [name]
  (when-not (empty? prev-theme)
    (dom/remove-class (dom/$ :#multi) prev-theme))
  (set! prev-theme (str "theme-" name))
  (dom/add-class (dom/$ :#multi) (str "theme-" name))
  (when-not (dom/$ (str "#theme-" name))
    (dom/append (dom/$ :head) (load-theme name))))

(defn get-themes []
  (sort-by #(.-text %)
           (for [[theme path] (object/raise-reduce app/app :themes+ {})]
              #js {:text (pr-str theme) :completion (pr-str theme)})))



(behavior ::provide-theme
                  :desc "Style: Provide editor theme"
