<commit_before>(ns expense-tracker.transaction.view
  (:require [expense-tracker.utils :as u]
            [expense-tracker.globals :as g]
            [expense-tracker.transaction.utils :as tu]
            [expense-tracker.account.rm :as ar]
            [expense-tracker.account.manage :as am]
            [clojure.string :as str]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; helpers

(defn filter-transactions [needle from to]
  (let [from (or from 19700101)
        to (or to (u/m-> (u/now) u/long-fmt))]
    (loop [trans @g/transactions
           rslt []]
      (if (empty? trans)
        rslt
        (recur (rest trans)
               (let [f (first trans)
                     tr (filter #(u/contains (:acc @%) needle) (:trans f))]
                 (if (and (not (empty? tr))
                          (<= from (:date f) to))
                   (conj rslt f)
                   rslt)))))))

(defn individual-accs [haystack typeof needle]
  (->> haystack
       ((fn [x]
          (if needle
            (filter #(and (= typeof (:type @%))
                          (u/contains (:acc @%) needle))
                    x)
            (filter #(= typeof (:type @%)) x))))
       (mapv (fn [x] [(:acc @x) (:val @x)]))))

(defn ch-sel [e href]
  (let [val (.-value (.-target e))
        now (u/now)
        today (u/m-> now u/long-fmt)
        m->long (fn [arg] (u/m-> arg u/long-fmt))]
    (condp = val
      "all" (u/trans-view e {:href href :to today :from 19700101})
      "cur-mon" (u/trans-view e {:href href :to today
                                 :from (m->long (.date now 1))})
      "last-30" (u/trans-view e {:href href :to today
                                 :from (m->long (.subtract now 30 "days"))})
      "last-60" (u/trans-view e {:href href :to today
                                 :from (m->long (.subtract now 60 "days"))})
      "last-90" (u/trans-view e {:href href :to today
                                 :from (m->long (.subtract now 90 "days"))})
      "cur-yr" (u/trans-view e {:href href :to today
                                :from (m->long (.month (.date now 1) 0))})
      "prev-mon" (let [prev (.subtract now 1 "months")]
                   (u/trans-view e {:href href
                                    :to (m->long (.date prev 31))
                                    :from (m->long (.date prev 1))}))
      "prev-yr" (let [prev (.subtract now 1 "years")]
                  (u/trans-view e {:href href
                                   :to (m->long (.month (.date prev 31) 12))
                                   :from (m->long (.month (.date prev 1) 0))}))
      "custom" (u/trans-view e {:href href}))))

(defn trans-rm [e t]
  (when (u/confirm "Do you really want to delete this transaction?")
    (tu/rm-helper t)
    (u/trans-view nil (:attrs @g/app-page))))

(defn trans-edit [e t] (reset! g/app-page {:page :trans-edit :attrs {:id (:id t)}}))

(defn acc-edit [e] )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; components and views

(defn c-acc [[href _]]
  [:a {:href "#" :onClick #(u/trans-view % {:href href}) :style {:margin-right "1em"}} href])

(defn c-view-transaction []
  (let [href (:href (:attrs @g/app-page))
        from (->> @g/app-page :attrs :from)
        to (->> @g/app-page :attrs :to)
        fltr (filter-transactions href from to)]
    [:div [:div [:div.form-group [:label "Breadcrumb: "]
                 (let [accs (str/split href #":")
                       cnt (count accs)]
                   [:span (for [i (range cnt)]
                            ^{:key (u/random)}
                            [:span [:a.text-capitalize
                                    {:href "#"<commit_msg>fix bug: trans-view: wrong usage of moment.js in ch-sel<commit_after>(ns expense-tracker.transaction.view
  (:require [expense-tracker.utils :as u]
            [expense-tracker.globals :as g]
            [expense-tracker.transaction.utils :as tu]
            [expense-tracker.account.rm :as ar]
            [expense-tracker.account.manage :as am]
            [clojure.string :as str]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; helpers

(defn filter-transactions [needle from to]
  (let [from (or from 19700101)
        to (or to (u/m-> (u/now) u/long-fmt))]
    (loop [trans @g/transactions
           rslt []]
      (if (empty? trans)
        rslt
        (recur (rest trans)
               (let [f (first trans)
                     tr (filter #(u/contains (:acc @%) needle) (:trans f))]
                 (if (and (not (empty? tr))
                          (<= from (:date f) to))
                   (conj rslt f)
                   rslt)))))))

(defn individual-accs [haystack typeof needle]
  (->> haystack
       ((fn [x]
          (if needle
            (filter #(and (= typeof (:type @%))
                          (u/contains (:acc @%) needle))
                    x)
            (filter #(= typeof (:type @%)) x))))
       (mapv (fn [x] [(:acc @x) (:val @x)]))))

(defn ch-sel [e href]
  (let [val (.-value (.-target e))
        now (u/now)
        today (u/m-> now u/long-fmt)
        m->long (fn [arg] (u/m-> arg u/long-fmt))]
    (condp = val
      "all" (u/trans-view e {:href href :to today :from 19700101})
      "cur-mon" (u/trans-view e {:href href :to today
                                 :from (m->long (.date now 1))})
      "last-30" (u/trans-view e {:href href :to today
                                 :from (m->long (.subtract now 30 "days"))})
      "last-60" (u/trans-view e {:href href :to today
                                 :from (m->long (.subtract now 60 "days"))})
      "last-90" (u/trans-view e {:href href :to today
                                 :from (m->long (.subtract now 90 "days"))})
      "cur-yr" (u/trans-view e {:href href :to today
                                :from (m->long (.month (.date now 1) 0))})
      ;; .subtract/.date mutates now/prev :((
      "prev-mon" (let [prev-to (.subtract (u/now) 1 "months")
                       prev-from (.subtract (u/now) 1 "months")]
                   (u/trans-view e {:href href
                                    :to (m->long (.date prev-to (.daysInMonth prev-to)))
                                    :from (m->long (.date prev-from 1))}))
      "prev-yr" (let [prev-to (.subtract (u/now) 1 "years")
                      prev-from (.subtract (u/now) 1 "years")]
                  (u/trans-view e {:href href
                                   :to (m->long (.date (.month prev-to 11) 31))
                                   :from (m->long (.date (.month prev-from 0) 1))}))
      "custom" (u/trans-view e {:href href}))))

(defn trans-rm [e t]
  (when (u/confirm "Do you really want to delete this transaction?")
    (tu/rm-helper t)
    (u/trans-view nil (:attrs @g/app-page))))

(defn trans-edit [e t] (reset! g/app-page {:page :trans-edit :attrs {:id (:id t)}}))

(defn acc-edit [e] )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; components and views

(defn c-acc [[href _]]
  [:a {:href "#" :onClick #(u/trans-view % {:href href}) :style {:margin-right "1em"}} href])

(defn c-view-transaction []
  (let [href (:href (:attrs @g/app-page))
        from (->> @g/app-page :attrs :from)
        to (->> @g/app-page :attrs :to)
        fltr (filter-transactions href from to)]
    [:div [:div [:div.form-group [:label "Breadcrumb: "]
                 (let [accs (str/split href #":")
                       cnt (count accs)]
                   [:span (for [i (range cnt)]
                            ^{:key (u/random)}
                            [:span [:a.text-capitalize
                                    {:href "#"
{'fraction_non_alphanumeric': 0.1927710843373494, 'fraction_numerical': 0.011262441068622316, 'mean_word_length': 1.558458961474037, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '926136', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2429, 'n_words': 708}