(ns buddho.dhamma.state
  (:require [buddho.core.dhamma.return :as ret]
            [buddho.core.state.idea :as idea])
  (:require-macros [buddho.macros.dhamma :refer [defdhamma]]))

(defdhamma have-idea
  "Create an idea out of data in a dhamma.
The [:have-idea :fields] metadata determines which fields are used.
Mind: pulls _fields_ from only *one* dhamma, but will pull _data_ from *both* dhamma!"
  {:meta {return-field :return-field
          fields :fields}
   :defaults {return-field :_latest}
   :data-as all-data}

  ;; TODO error if no fields specified
  (let [idea (idea/have-idea!)
       ; fields (or fields (keys all-data))
        data (zipmap fields (map all-data fields))]
    (idea/update-idea! idea data)
    (ret/with-return parent :have-idea
      {return-field idea})))
