<commit_before>(ns omnext-routing-one.core
  (:require-macros
    [cljs.core.async.macros :refer (go-loop)]
    )
  (:require [goog.dom :as gdom]
            [om.next :as om :refer-macros [defui]]
            [om.dom :as dom]
            [clojure.string :as string]
            [cljs.core.async :refer (<! put! chan)]
            ))

(enable-console-print!)

;; URL helper functions

(defn parse-url-hash [h]
  "#/foo/bar/?a=b&c=d -> {:path [\"foo\" \"bar\"], :params {:a \"b\", :c \"d\"}}"
  (let [
      h (subs h 2)
      [path params] (string/split h #"\u003f")
      path (string/split path \/) ; TODO: filter identity ???
      params (into {} (map (comp
                    (fn [[k v]] [(keyword (js/decodeURIComponent k))
                                 (if v
                                   (js/decodeURIComponent v)
                                   v
                                   )
                                 ])
                    #(string/split % "=")) (string/split params "&")))
      ]
    {:path path
     :params params}
    )
  )

(defn path-and-params-to-hash
  "Opposite for function from above. Expects keys of `params` to be keywords"
  ([path]
   (path-and-params-to-hash path nil)
   )
  ([path params]
    (apply str "#/" (string/join "/" path) "/"
         (when params
           (cons
             "?"
            (string/join "&" (map (fn [[k v]]
                                    (str
                                      (-> k name js/encodeURIComponent)
                                      "="
                                      (-> v js/encodeURIComponent)
                                      )
                                    ) params))
            )
           )
         )
   )
  )

;; Tab functions

(defn get-tab-by-parsed-url [tabs {:keys [path params]}]
  "Tabs format is defined in `tabs` below. See `parse-url-hash` for second param"
  (first (filter
           #(= (:url-path %) path)
           tabs))
  )

(defn get-query-and-params-by-parsed-url [tabs parsed-url]
  (if-let [component (some->>
    (get-tab-by-parsed-url tabs parsed-url)
    :component
    )]
      {:query (om/query component)
       :params (let [component-params (om/params component)]
                 (merge
                   component-params
                   (select-keys
                     (:params parsed-url)
                     (keys component-params)
                     )))
       }
    )
  )


;; Tab components

(defui CountriesTab
  static om/IQueryParams
  (params [_]
          {:page 1}
          )

  static om/IQuery
  (query [_]
         '[:tab-key/countries
           (:countries {:page ?page})]<commit_msg>comment<commit_after>(ns omnext-routing-one.core
  (:require-macros
    [cljs.core.async.macros :refer (go-loop)]
    )
  (:require [goog.dom :as gdom]
            [om.next :as om :refer-macros [defui]]
            [om.dom :as dom]
            [clojure.string :as string]
            [cljs.core.async :refer (<! put! chan)]
            ))

(enable-console-print!)

;; URL helper functions

(defn parse-url-hash [h]
  "#/foo/bar/?a=b&c=d -> {:path [\"foo\" \"bar\"], :params {:a \"b\", :c \"d\"}}"
  (let [
      h (subs h 2)
      [path params] (string/split h #"\u003f")
      path (string/split path \/) ; TODO: filter identity ???
      params (into {} (map (comp
                    (fn [[k v]] [(keyword (js/decodeURIComponent k))
                                 (if v
                                   (js/decodeURIComponent v)
                                   v
                                   )
                                 ])
                    #(string/split % "=")) (string/split params "&")))
      ]
    {:path path
     :params params}
    )
  )

(defn path-and-params-to-hash
  "Opposite for function from above. Expects keys of `params` to be keywords"
  ([path]
   (path-and-params-to-hash path nil)
   )
  ([path params]
    (apply str "#/" (string/join "/" path) "/"
         (when params
           (cons
             "?"
            (string/join "&" (map (fn [[k v]]
                                    (str
                                      (-> k name js/encodeURIComponent)
                                      "="
                                      (-> v js/encodeURIComponent)
                                      )
                                    ) params))
            )
           )
         )
   )
  )

;; Tab functions

(defn get-tab-by-parsed-url [tabs {:keys [path params]}]
  "Tabs format is defined in `tabs` below. See `parse-url-hash` for second param"
  (first (filter
           #(= (:url-path %) path)
           tabs))
  )

(defn get-query-and-params-by-parsed-url [tabs parsed-url]
  (if-let [component (some->>
    (get-tab-by-parsed-url tabs parsed-url)
    :component
    )]
      {:query (om/query component)
       :params (let [component-params (om/params component)]
                 (merge
                   component-params
                   (select-keys
                     (:params parsed-url)
                     (keys component-params)
                     )))
       }
    )
  )


;; Tab components

(defui CountriesTab
  static om/IQueryParams
  (params [_]
          {:page 1}
          )

  static om/IQuery
  (query [_]
         '[:tab-key/countries ; XXX: this is crazy hack, which allows
                              ; to determine active tab by it's query
           (:countries {:page ?page})]
{'fraction_non_alphanumeric': 0.13771031455742502, 'fraction_numerical': 0.0018288222384784199, 'mean_word_length': 1.1972679791080756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6255225', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1619, 'n_words': 383}