(ns auto-ap.views.pages.transactions.common
  (:require
   [auto-ap.subs :as subs]
   [re-frame.core :as re-frame]))

(def transaction-read
  [:id
   :amount
   :memo
   :location
   :approval-status
   :check-number
   :is-locked

   [:matched-rule [:note :id]]
   [:vendor [:name :id]]
   [:accounts [:id :amount :location [:account [:name :id :location :numeric-code]]]]
   :date
   [:yodlee_merchant [:name :yodlee-id :id]]
   [:plaid_merchant [:name :id]]
   :post_date
   [:expected-deposit [:id :date]]
   [:forecast-match [:id :identifier]]
   :status
   :description_original
   [:payment [:check_number :s3_url :id :date]]
   [:client [:name :id]]
   [:bank-account [:name :yodlee-account-id :current-balance]]])

(defn data-params->query-params [params]
  (if (:exact-match-id params)
    {:exact-match-id       (some-> (:exact-match-id params) str)}
    {:start                (:start params 0)
     :per-page             (:per-page params)
     :sort                 (:sort params)
     :vendor-id            (:id (:vendor params))
     :date-range           (:date-range params)
     :account-id           (:id (:account params))
     :bank-account-id      (:id (:bank-account params))
     :amount-gte           (:amount-gte (:amount-range params))
     :exact-match-id       (some-> (:exact-match-id params) str)
     :unresolved           (:unresolved params)
     :potential-duplicates (:potential-duplicates params)
     :location             (:location params)
     :import-batch-id      (some-> (:import-batch-id params) str)
     :amount-lte           (:amount-lte (:amount-range params))
     :linked-to      (:linked-to params)
     :description          (:description params)
     :approval-status      (condp = @(re-frame/subscribe [::subs/active-page])
                             :transactions                   nil
                             :unapproved-transactions        :unapproved
                             :requires-feedback-transactions :requires-feedback
                             :excluded-transactions          :excluded
                             :approved-transactions          :approved)}))
