;; Extension — document composition.

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

(define
  a
  (doc-with-title
    (doc-append (doc-empty "a") (mk-heading "h" 1 "A"))
    "Doc A"))
(define
  b
  (doc-append
    (doc-append (doc-empty "b") (mk-text "p" "B1"))
    (mk-text "q" "B2")))

;; ── concat ──
(define ab (content/concat a b))
(content-test "concat ids" (doc-ids ab) (list "h" "p" "q"))
(content-test "concat keeps first id" (doc-id ab) "a")
(content-test "concat keeps first title" (doc-title ab) "Doc A")
(content-test "concat immutable a" (doc-ids a) (list "h"))
(content-test "concat immutable b" (doc-ids b) (list "p" "q"))

;; ── prepend ──
(define ba (content/prepend a b))
(content-test "prepend ids" (doc-ids ba) (list "p" "q" "h"))
(content-test "prepend keeps a id" (doc-id ba) "a")

;; ── concat with empty ──
(content-test
  "concat empty right"
  (doc-ids (content/concat a (doc-empty "e")))
  (list "h"))
(content-test
  "concat empty left"
  (doc-ids (content/concat (doc-empty "e") b))
  (list "p" "q"))

;; ── concat-all ──
(define c (doc-append (doc-empty "c") (mk-divider "d")))
(content-test
  "concat-all order"
  (doc-ids (content/concat-all (list a b c)))
  (list "h" "p" "q" "d"))
(content-test
  "concat-all keeps first id"
  (doc-id (content/concat-all (list a b c)))
  "a")
(content-test
  "concat-all single"
  (doc-ids (content/concat-all (list a)))
  (list "h"))
(content-test
  "concat-all empty"
  (doc-ids (content/concat-all (list)))
  (list))

;; ── render of composed doc ──
(content-test
  "composed renders"
  (asHTML (content/concat a b))
  "<h1>A</h1><p>B1</p><p>B2</p>")

;; ── wrap-section collapses blocks into a subtree ──
(define w (content/wrap-section ab "sec"))
(content-test "wrap top-level is one section" (doc-ids w) (list "sec"))
(content-test
  "wrap children preserved"
  (doc-tree-ids w)
  (list "sec" "h" "p" "q"))
(content-test
  "wrap renders nested"
  (asHTML w)
  "<section><h1>A</h1><p>B1</p><p>B2</p></section>")
