 (ns server.parsing.copaste
+  (:import [om.tempid TempId])
   (:require [clj-consonant.store :as s]
             [server.parser :refer [mutatef readf]]
             [server.transaction :as t]))


(defmethod mutatef 'copaste/save-snippet
  [{:keys [consonant]} _ {:keys [ref snippet]}]
  {:value {:keys [:copaste/refs :copaste/snippets]
           :tempids {}}
   :action (fn []
             (let [ref (if ref ref (s/get-ref consonant "HEAD"))
                   snippet (om->object snippet)]
               (if (:uuid snippet)
                 (update-snippet consonant ref snippet)
                 (create-snippet consonant ref snippet))))})

;;;; Reads

(defmethod readf :copaste/refs
  [{:keys [consonant query]} _ _]
  {:value (->> (s/get-refs consonant)
               vals
               (map #(select-keys % query))
               (into []))})

(defmethod readf :copaste/snippets
  [{:keys [consonant query]} _ {:keys [ref]}]
  {:value (->> (s/get-objects consonant (second ref) "snippet")
               (map object->om)
               (map #(select-keys % query))
               (into []))})

;;;; Mutations
