 ;;       [:success-event "my-token" result]
 
 
-(defn ajax-handler
+(defn ajax-xhrio-handler
   "ajax-request only provides a single handler for success and errors"
   [on-success on-failure xhrio [success? response]]
   ; see http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html

      (on-failure details))))



(defn spec->ajax-options
  [{:as   request
    :keys [on-success on-failure api response-format]
    :or   {on-success      [:http-no-on-success]
           on-failure      [:http-no-on-failure]
           api             (new js/goog.net.XhrIo)
           response-format (ajax/detect-response-format)}}]
  ; wrap events in cljs-ajax callbacks
  (-> request
      (assoc
        :api             api
        :response-format response-format
        :handler        (partial ajax-handler
                                 #(dispatch (conj on-success %))
                                 #(dispatch (conj on-failure %))
                                 api))
      (dissoc :on-success :on-failure)))

(s/def ::sequential-or-map (s/or :list-or-vector sequential? :map map?))

(def-fx
  :http
  (fn http-effect [request]
    (when (= :cljs.spec/invalid (s/conform ::sequential-or-map request))
      (console :error (s/explain-str ::sequential-or-map request)))
    ;;TODO verify detail request(s) using spec for ajax-request api
    (cond
      (sequential? request) (doseq [each request] (http-effect each))
      (map? request)        (-> request spec->ajax-options ajax/ajax-request)
      :else
      (console :error "re-frame-http-fx: expected request to be a list or vector or map, but got: " request))))

    [ajax.core          :as ajax]
    [cljs.spec          :as s]))

;; I provide the :http-xhrio effect handler leveraging cljs-ajax lib
;; see API docs https://github.com/JulianBirch/cljs-ajax
;; Note we use the ajax-request.
;;
;; Deviation from cljs-ajax options in request
;; :handler       - not supported, see :on-success and :on-failure
;; :on-success    - event vector dispatched with result
;; :on-failure    - event vector dispatched with result
