             [com.wsscode.pathom.book.connect.mutation-join]
             [com.wsscode.pathom.book.connect.mutation-join-globals]
             [com.wsscode.pathom.book.connect.mutation-async]
-            [com.wsscode.pathom.book.ui.codemirror :as cm]
             [com.wsscode.pathom.fulcro.network :as network]
-            [com.wsscode.pathom.specs.query :as s.query]
+            [com.wsscode.pathom.viz.query-editor :as pv.query-editor]
             [fulcro.client :as fulcro]
             [fulcro.client.localized-dom :as dom]
-            [fulcro.client.data-fetch :as df]
-            [fulcro.client.mutations :as fm]
             [fulcro.client.primitives :as fp]
             [clojure.string :as str]))
-
-(s/def ::parser ::p/parser)
-(s/def ::env ::p/env)
 
 (def parsers
   {"async.intro"                   {::parser com.wsscode.pathom.book.async.intro/parser}

(ns com.wsscode.pathom.book.interactive-parser
  (:require [clojure.reader :refer [read-string]]
            [cljs.pprint]
            [cljs.spec.alpha :as s]
            [com.wsscode.pathom.core :as p]
            [com.wsscode.pathom.book.app-types :as app-types]
            [com.wsscode.pathom.book.async.intro]
            [com.wsscode.pathom.book.async.error-propagation]

    (str/replace s (js/RegExp (str ":" ns "\\/")) "::")
    s))

(fp/defsc QueryEditorWrapper
  [this {:ui/keys [root]}]
  {:initial-state (fn [query]
                    {:ui/root (-> (fp/get-initial-state pv.query-editor/QueryEditor {})
                                  (assoc ::pv.query-editor/query query))})
   :query         [{:ui/root (fp/get-query pv.query-editor/QueryEditor)}]
   :css           [[:.container {:height  "500px"
                                 :display "flex"}]]
   :css-include   [pv.query-editor/QueryEditor]}
  (dom/div :.container
    (pv.query-editor/query-editor root {::pv.query-editor/default-trace-size 200})))

(def query-editor-wrapper (fp/factory QueryEditorWrapper {:keyfn ::id}))

(app-types/register-app "interactive-parser"
  (fn [{::app-types/keys [node]}]
    (let [parser-name   (.getAttribute node "data-parser")
          initial-query (.-innerText node)]
      (let [{::keys [parser ns] :as iparser} (get parsers parser-name)]
        (assert iparser (str "parser " parser-name " not foud"))
        {::app-types/app
         (fulcro/new-fulcro-client
           :initial-state (-> (fp/get-initial-state QueryEditorWrapper initial-query)
                              (assoc :fulcro.inspect.core/app-id (str "query-editor-" parser-name)))

           :networking {pv.query-editor/remote-key
                        (network/pathom-remote
                          (pv.query-editor/client-card-parser parser
                            {::pv.query-editor/wrap-run-query
                             (fn [run-query]
                               (fn [env input]
                                 (go-catch
                                   (-> (run-query env (update input ::pv.query-editor/query #(expand-keywords % ns))) <?
                                       (update ::pv.query-editor/result #(compact-keywords % ns))))))}))})

         ::app-types/root
         QueryEditorWrapper}))))
