 (ns labradoodle.excel
-  (:use [dk.ative.docjure.spreadsheet]))
+  (:use [dk.ative.docjure.spreadsheet])
+  (:require [clojure.string :as s :refer [trim join split]])
+  (:import (clojure.lang ISeq)))
 
 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ;; Referenced while coding this ns:

      [k (get-in v (ks->path colk) v)]
      entry)))

(defn issues->rows [cols issues]
  "Produces rows array, with shape like this:
  [{:name \"Foo Widget\" :price 100}
   {:name \"Bar Widget\" :price 200}]"
  ; Create rows by transforming  issue objects as follows:
  ; (A) only contain keys from cols map;
  ; (B) resolves nested values for path keys (e.g. replaces milestone object with title if key is [:milestone :title])
  ; (C) values are all strings
  ; (D) entries are in same order as cols entries
  (let [topkeys (cols->topkeys cols)
        transform-issue (comp
                          #(into (sorted-map-by (partial idx-comparator topkeys)) %)
                          ;#(into (sorted-map) %)
                          #(fmap str %)
                          #(map (partial resolve-nested-val cols) %)
                          #(select-keys % topkeys))
        rows (map transform-issue issues)]
    (println "---------------")
    (clojure.pprint/pprint rows)
    rows))

(defn issues->celldata [issues]
  "Create celldata array, with shape like this:

;;  - http://stackoverflow.com/questions/1676891/mapping-a-function-on-the-values-of-a-map-in-clojure
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Cell Data Definition
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(def columns (array-map :group ""
                        :id "ID"
                        :title "Outstanding Item",
                        :description "Notes",
                        :labels "Tags"
                        [:milestone :title] "Milestone",
                        :state "Status",
                        [:assignee :name] "Assigned To"))

; groups are based on Issue labels
(def groups [{:display "Development" :label "dev"},
             {:display "Ops" :label "ops"},
             {:display "Demo Prep" :label "demo"},
             {:display "Documentation" :label "docs"}])

;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Data Transformation functions
;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(defn fmap [f m]
  "Based on corresponding multi-method in https://github.com/clojure/algo.generic"
