;; Extension — on-the-wire serialization (to-wire / from-wire).

(st-bootstrap-classes!)
(content/bootstrap!)
(content-bootstrap-text!)
(content-bootstrap-section!)
(content-bootstrap-table!)

(define
  d
  (doc-with-meta
    (doc-append
      (doc-append (doc-empty "post") (mk-heading "h" 1 "Title"))
      (mk-text "p" "Body text"))
    {:title "T" :tags (list "x" "y")}))

;; ── to-wire produces a string ──
(content-test "to-wire is string" (string? (content/to-wire d)) true)

;; ── parse(to-wire) == data form ──
(content-test
  "wire parses to data"
  (parse (content/to-wire d))
  (content/to-data d))

;; ── round-trip preserves everything ──
(define rt (content/wire-round-trip d))
(content-test "rt id" (doc-id rt) "post")
(content-test "rt title" (doc-title rt) "T")
(content-test "rt tags" (doc-tags rt) (list "x" "y"))
(content-test "rt ids" (doc-ids rt) (list "h" "p"))
(content-test "rt render" (asHTML rt) (asHTML d))

;; ── nested + table survive the wire ──
(define
  dn
  (doc-append
    (doc-append
      (doc-empty "d")
      (mk-section "s" (list (mk-text "a" "deep"))))
    (mk-table "t" (list "A") (list (list "1")))))
(content-test
  "wire nested render"
  (asHTML (content/wire-round-trip dn))
  (asHTML dn))
(content-test
  "wire nested tree-ids"
  (doc-tree-ids (content/wire-round-trip dn))
  (doc-tree-ids dn))

;; ── empty doc ──
(content-test
  "wire empty"
  (doc-ids (content/from-wire (content/to-wire (doc-empty "e"))))
  (list))

;; ── from-wire of an externally-built wire string ──
(content-test
  "from-wire external"
  (asHTML
    (content/from-wire
      "{:id \"x\" :blocks ({:id \"h\" :type \"heading\" :fields {:level 2 :text \"Hi\"}})}"))
  "<h2>Hi</h2>")
