;; _hyperscript playground API handler

(defhandler
  :path "/sx/(applications.(hyperscript.(api.compile)))"
  :method :post
  :csrf false
  :returns "element"
  (&key source)
  (if
    (or (nil? source) (empty? source))
    (p
      (~tw :tokens "text-sm text-gray-400 italic")
      "Enter some hyperscript and click Compile.")
    (let
      ((compiled (hs-to-sx-from-source source)))
      (div
        (~tw :tokens "space-y-4")
        (div
          (h4
            (~tw
              :tokens "text-xs font-semibold uppercase tracking-wider text-gray-500 mb-2")
            "Compiled SX")
          (pre
            (~tw
              :tokens "bg-gray-900 text-green-400 p-4 rounded-lg text-sm overflow-x-auto whitespace-pre-wrap font-mono")
            (sx-serialize compiled)))
        (div
          (h4
            (~tw
              :tokens "text-xs font-semibold uppercase tracking-wider text-gray-500 mb-2")
            "Parse Tree")
          (pre
            (~tw
              :tokens "bg-gray-900 text-amber-400 p-4 rounded-lg text-sm overflow-x-auto whitespace-pre-wrap font-mono")
            (sx-serialize (hs-compile source))))))))