(ns buddho.dhamma.state
  (:require [buddho.core.dhamma :as d]
            [buddho.core.meta :as meta]
            [buddho.core.state :as s]
            [sekai.math.vectors :as vect])
  (: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 (s/have-idea!)
       ; fields (or fields (keys all-data))
        data (zipmap fields (map all-data fields))]
    (s/update-idea! idea data)
    (d/with-return parent :have-idea
      {return-field idea})))
