         (assoc :nodes nodes)
         (assoc :connections connections))))
 
+
 (defn- write-connect-destinations [node-graph to-node-id]
-  (->> to-node-id
-       (node-graph/nodes-in-to node-graph)
+  (->> (node-graph/nodes-in-to node-graph to-node-id)
        (map (fn [[[from-id _] _]]
               (str "    this." (node-id->var from-id)
                    ".connect(destination, input);")))
        (string/join "\n")))
 
 (defn- write-disconnect-destinations [node-graph to-node-id]
-  (->> to-node-id
-       (node-graph/nodes-in-to node-graph)
+  (->> (node-graph/nodes-in-to node-graph to-node-id)
        (map (fn [[[from-id _] _]]
               (str "    this." (node-id->var from-id)
                    ".disconnect();")))
        (string/join "\n")))
 
 (defn- write-output-if-statements [node-graph f]
-  (->> node-graph
-       :nodes
-       (filter (fn [[_ n]] (-> n node/node-type :export-data :type (= :output))))
+  (->> (:nodes node-graph)
+       (filter (fn [[_ n]] (-> n node-export-data-type (= :output))))
        (map-indexed (fn [i [id _]]
                       (str "  if (output === " i ") {\n"
                            (f node-graph id) "\n"

       (map write-dependency)
       (string/join "\n")))

(defn- write-set-input [node-id [_ input]]
  (case (:type input)
    :param (str "  " (node-id->var node-id)
                "." (:name input)
                ".value = " (:default input) ";")
    :constant (str "  " (node-id->var node-id)
                   "." (:name input)
                   " = " (pr-str (:default input)) ";")
    nil))

(defn- write-create-node [[node-id node]]
  (let [construct (str "  var " (node-id->var node-id)
                       " = "
                       (-> node node/node-type :export-data :constructor) ";")
        inputs (-> node node/node-type :inputs)
        props (string/join "\n"
                           (keep (partial write-set-input node-id)
                                 inputs))]
    (str construct "\n" props (if (string/blank? props) "" "\n"))))

(defn- write-create-nodes [node-graph]
  (->> node-graph
       :nodes
       (filter (fn [[_ n]] (-> n node/node-type :export-data :constructor)))
       (map write-create-node)
       (string/join "\n")))

(defn- write-connection [node-graph [[from-node-id from-slot-id] [to-node-id to-slot-id]]]
  (let [from-node-var (node-id->var from-node-id)
        to-node-var (node-id->var to-node-id)

        from-slot (-> node-graph :nodes from-node-id node/node-type :outputs from-slot-id)
        to-slot (-> node-graph :nodes to-node-id node/node-type :inputs to-slot-id)]

    (when-not (= (:type from-slot) :null-node)
      (case (:type to-slot)
        :param (str "  " from-node-var
                    ".connect(" to-node-var "." (:name to-slot)
                    ", " (:index from-slot)
                    ");")
        :node (str "  " from-node-var
                   ".connect(" to-node-var
                   ", " (:index from-slot)
                   ");")
        :gate (str "  " from-node-var
                   ".connect(" to-node-var
                   ", " (:index from-slot)
                   ");")
        :null-node nil))))

(defn- write-connections [node-graph]
  (->> node-graph
       :connections
       (map (partial write-connection node-graph))
       (remove nil?)
       (string/join "\n")))

(defn- write-property-input [node-graph [from-node-id _]]
  (->> from-node-id
       (node-graph/nodes-out-from node-graph)
       (map (fn [[_ [to-id to-slot-id]]]
              [to-id (-> node-graph :nodes to-id node/node-type :inputs to-slot-id)]))
       (filter (fn [[node-id slot]] (#{:param :node} (:type slot))))
       (map (fn [[node-id slot]]
              (str "  this." (or (:name slot) (node-id->var node-id))
                   " = " (node-id->var node-id) (if (= (:type slot) :param)

(defn- node-id->var [node-id]
  (string/replace (str node-id) #"[:-]" ""))

(defn- node-graph-node [node-graph node-id]
  (-> node-graph :nodes node-id))

(defn- node-export-data [node]
  (-> node node/node-type :export-data))

(defn- node-dependencies [node]
  (-> node node-export-data :dependencies))

(defn- node-constructor [node]
  (-> node node-export-data :constructor))

(defn- node-export-data-type [node]
  (-> node node-export-data :type))

(defn- node-ignores-export? [node]
  (-> node node-export-data :ignore-export))

(defn- node-inputs [node]
  (-> node node/node-type :inputs))

(defn- node-outputs [node]
  (-> node node/node-type :outputs))

(defn- node-input [node input-id]
  (-> node node-inputs input-id))

(defn- node-output [node output-id]
  (-> node node-outputs output-id))


(defn- write-dependency [[class-name [file _]]]
  (str "var " class-name " = require('" file "');"))

(defn- write-dependencies [node-graph]
  (->> (:nodes node-graph)
