<commit_before>(ns clj-chrome-devtools.impl.define
  "Macro for defining a domain of commands."
  (:require [clj-chrome-devtools.protocol-definitions :as proto]
            [clj-chrome-devtools.impl.connection :as connection]
            [clojure.string :as str]
            [clojure.core.async :as async :refer [go <!! >!]]
            [clojure.set :as set]
            [clj-chrome-devtools.impl.util :refer [camel->clojure]]
            [clojure.spec.alpha :as s]))


(def to-symbol (comp symbol camel->clojure))

(defonce command-id (atom 0))

(defn next-command-id! []
  (swap! command-id inc))

(defn command-payload [id name params parameter-names]
  {:id id
   :method name
   :params (set/rename-keys params parameter-names)})

(defn process-doc [doc]
  (some-> doc
          (str/replace #"<code>" "`")
          (str/replace #"</code>" "`")))

(defn- describe-map-keys [ks]
  (let [max-key-length (reduce max 0 (map (comp count camel->clojure :name) ks))]
    (str "\n\n  Key " (str/join (repeat (- max-key-length 2) " ")) "| Description "
         "\n  " (str/join (repeat (+ 2 max-key-length) "-")) "|------------ \n"
         (str/join "\n"
                   (map #(str (format (str "  :%-" max-key-length "s | %s")
                                      (camel->clojure (:name %))
                                      (process-doc (:description %)))
                              (when (:optional %)
                                " (optional)"))
                        ks)))))

(def kw (comp keyword camel->clojure))

(defn ns-kw [string]
  (keyword (str *ns*) (camel->clojure string)))

(defn keys-spec [properties]
  (let [{opt-un true req-un false} (group-by (comp boolean :optional) properties)]
    `(s/keys ~@(when-not (empty? req-un)
                 `(:req-un [~@(map (comp ns-kw :name) req-un)]))
             ~@(when-not (empty? opt-un)
                 `(:opt-un [~@(map (comp ns-kw :name) opt-un)])))))

(defn spec-for-type [{type :type :as t}]
  (if (:enum t)
    ;; Enumerated type, just output the set of valid values
    (into #{} (:enum t))

    (case type
      "object" (keys-spec (:properties t))
      "string" `string?
      "integer" `integer?
      "number" `number?
      "boolean" `boolean?
      "array" `(s/coll-of ~(spec-for-type (:items t))))))

(defmacro define-type-specs [domain]
  `(do
     ~@(for [{id :id type :type :as t} (proto/types-for-domain domain)
             :let [name-kw (ns-kw id)]]

         `(do
            ;; If this is an object, create specs for basic type keys
            ~@(for [{name :name :as property-type} (:properties t)
                    :when (#{"string" "number" "integer" "boolean" "array"} type)]
                `(s/def ~(ns-kw name)
                   ~(spec-for-type property-type)))
            (s/def ~name-kw ~(spec-for-type t))))))

(define-type-specs "DOM")

(defmacro define-command-functions [domain]<commit_msg>Remove debug statement<commit_after>(ns clj-chrome-devtools.impl.define
  "Macro for defining a domain of commands."
  (:require [clj-chrome-devtools.protocol-definitions :as proto]
            [clj-chrome-devtools.impl.connection :as connection]
            [clojure.string :as str]
            [clojure.core.async :as async :refer [go <!! >!]]
            [clojure.set :as set]
            [clj-chrome-devtools.impl.util :refer [camel->clojure]]
            [clojure.spec.alpha :as s]))


(def to-symbol (comp symbol camel->clojure))

(defonce command-id (atom 0))

(defn next-command-id! []
  (swap! command-id inc))

(defn command-payload [id name params parameter-names]
  {:id id
   :method name
   :params (set/rename-keys params parameter-names)})

(defn process-doc [doc]
  (some-> doc
          (str/replace #"<code>" "`")
          (str/replace #"</code>" "`")))

(defn- describe-map-keys [ks]
  (let [max-key-length (reduce max 0 (map (comp count camel->clojure :name) ks))]
    (str "\n\n  Key " (str/join (repeat (- max-key-length 2) " ")) "| Description "
         "\n  " (str/join (repeat (+ 2 max-key-length) "-")) "|------------ \n"
         (str/join "\n"
                   (map #(str (format (str "  :%-" max-key-length "s | %s")
                                      (camel->clojure (:name %))
                                      (process-doc (:description %)))
                              (when (:optional %)
                                " (optional)"))
                        ks)))))

(def kw (comp keyword camel->clojure))

(defn ns-kw [string]
  (keyword (str *ns*) (camel->clojure string)))

(defn keys-spec [properties]
  (let [{opt-un true req-un false} (group-by (comp boolean :optional) properties)]
    `(s/keys ~@(when-not (empty? req-un)
                 `(:req-un [~@(map (comp ns-kw :name) req-un)]))
             ~@(when-not (empty? opt-un)
                 `(:opt-un [~@(map (comp ns-kw :name) opt-un)])))))

(defn spec-for-type [{type :type :as t}]
  (if (:enum t)
    ;; Enumerated type, just output the set of valid values
    (into #{} (:enum t))

    (case type
      "object" (keys-spec (:properties t))
      "string" `string?
      "integer" `integer?
      "number" `number?
      "boolean" `boolean?
      "array" `(s/coll-of ~(spec-for-type (:items t))))))

(defmacro define-type-specs [domain]
  `(do
     ~@(for [{id :id type :type :as t} (proto/types-for-domain domain)
             :let [name-kw (ns-kw id)]]

         `(do
            ;; If this is an object, create specs for basic type keys
            ~@(for [{name :name :as property-type} (:properties t)
                    :when (#{"string" "number" "integer" "boolean" "array"} type)]
                `(s/def ~(ns-kw name)
                   ~(spec-for-type property-type)))
            (s/def ~name-kw ~(spec-for-type t))))))

(defmacro define-command-functions [domain]
{'fraction_non_alphanumeric': 0.18068965517241378, 'fraction_numerical': 0.001379310344827586, 'mean_word_length': 2.021354166666667, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3091997', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1913, 'n_words': 513}