(ns buddho.core.dhamma.return
  "Facilities for returning values to parent dhamma."
  (:require [buddho.core.dhamma.data :as data]))

;; TODO rewrite this module because it's weird shit

(defn return-value
  "Get return value for `field` and key sequence `keys` on `dhamma`.
  Defaults to last modified value whenever possible."
  [dhamma & [field & keys]]
  (apply data/get-meta
         (or dhamma {})
         (concat [(or field
                      (data/get-meta dhamma :_latest_return))]
                 [:_return] (or keys [:_latest]))))

;; TODO always set latest
(defn with-return
  "Set the return `value` for `field` on `dhamma`."
  [dhamma field value]
  (let [dh (data/assoc-meta (or dhamma {}) :_latest_return field)]
    (data/assoc-meta
     dh
     (conj [field] :_return)
     (merge (return-value dhamma field) value))))
