 (ns clj-spotify.core
   (:require [clj-http.client :as client]
             [clojure.data.json :as json]
-            [clojure.string :as string]
-            )
-  )
+            [clojure.string :as string]))
 
 (def template-keys [:id :category_id :owner_id :playlist_id :user_id])
 

  (json/read-str s :key-fn keyword))

(defn response-to-map
  "Parse body of http respose to json"
  [response]
  (with-meta
    (try
      (json-string-to-map (:body response))
      (catch java.lang.NullPointerException e {:error {:status "NullPointerException"  :message (.getMessage e)}})
      (catch Exception e {:error {:status "Exception"  :message (.getMessage e)}})
      ) {:status (:status response)}))

(defn- build-new-url
  "Do the building nescessary in replace-url-values here. url-parts is the split up url which values works as keys in param-map."
  [url url-parts param-map]
  (if (empty? url-parts)
    (str "https:/" url)
    (let [url-key (keyword (last url-parts))
          new-url (str "/" (if (contains? param-map url-key)
                             (url-key param-map)
                             (last url-parts))  url)]
      (recur new-url (pop url-parts) param-map))))

(defn replace-url-values

  [param-map url]
  (if (string/blank? url)
    "" 
    (let [act-tmplt-keys (select-keys param-map template-keys)
          split-url (string/split (string/replace url "https://" "") #"/")]
      (build-new-url "" split-url act-tmplt-keys))))

(defn comma-separate
  "If v is a sequence, convert to a comma separated string." 
  [v]
  (if (or (seq? v) (vector? v))
    (string/join "," v)
    v))

(defn- map-vals
  "Applies f to each value in m."
  [f m]
  (zipmap (keys m)
          (map f (vals m))))

(defn convert-values
  "Convert values in query-params to match spotifys api."
  [m]
  (map-vals comma-separate m))

(defn remove-path-keys
  "Remove keys that are used in modifying the path."
