;; Hyperscript debug harness — mock DOM for instant testing
;;
;; Load once into the image, then repeatedly call hs-run.
;; All DOM ops are intercepted and logged via the test harness.

;; ── Mock element ────────────────────────────────────────────────

(define
  hs-mock-element
  (fn
    (tag id classes)
    (let
      ((cls-set (reduce (fn (d c) (dict-set d c true)) {} classes)))
      {:children () :_hs-activated true :tag tag :classes cls-set :text "" :id id :attrs {}})))

;; ── Mock platform ───────────────────────────────────────────────

(define hs-mock-platform {:hs-wait (fn (ms) nil) :hs-wait-for (fn (target event) nil) :dom-get-attr (fn (el attr) (get (get el "attrs") attr)) :dom-has-class? (fn (el cls) (dict-has? (get el "classes") cls)) :dom-set-text (fn (el text) (dict-set! el "text" text) nil) :hs-settle (fn (el) nil) :dom-add-class (fn (el cls) (dict-set! (get el "classes") cls true) nil) :dom-query (fn (sel) nil) :dom-remove-class (fn (el cls) (dict-delete! (get el "classes") cls) nil) :dom-listen (fn (target event-name handler) (handler {:target target :type event-name})) :dom-set-attr (fn (el attr val) (dict-set! (get el "attrs") attr val) nil) :dom-query-all (fn (sel) ())})

;; ── Convenience runner ──────────────────────────────────────────

(define
  hs-run
  (fn
    (src)
    (let
      ((me (hs-mock-element "div" "test" ()))
        (sx (hs-to-sx-from-source src)))
      (let
        ((handler (eval-expr (list (quote fn) (quote (me)) (list (quote let) (quote ((it nil) (event {:target me :type "click"}))) sx)))))
        (handler me)
        me))))

;; ── Element inspection ──────────────────────────────────────────

(define hs-classes (fn (el) (keys (get el "classes"))))

(define hs-has-class? (fn (el cls) (dict-has? (get el "classes") cls)))
