;; Extension — block id remapping / clone.

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

(define
  d
  (doc-append
    (doc-append (doc-empty "d") (mk-heading "h" 1 "Title"))
    (mk-section "s" (list (mk-text "a" "A") (mk-text "b" "B")))))

;; ── prefix-ids rewrites every id in the tree ──
(define p (content/prefix-ids d "x-"))
(content-test "prefix top-level ids" (doc-ids p) (list "x-h" "x-s"))
(content-test
  "prefix tree-ids"
  (doc-tree-ids p)
  (list "x-h" "x-s" "x-a" "x-b"))
(content-test "prefix immutable" (doc-tree-ids d) (list "h" "s" "a" "b"))
(content-test "prefix preserves content" (asHTML p) (asHTML d))
(content-test
  "prefix preserves nested content"
  (str (blk-send (doc-deep-find p "x-a") "text"))
  "A")

;; ── custom remap fn ──
(define u (content/remap-ids d (fn (id) (str id "!"))))
(content-test "remap suffix" (doc-tree-ids u) (list "h!" "s!" "a!" "b!"))

;; ── collision-free composition ──
(define
  d2
  (doc-append (doc-empty "d2") (mk-heading "h" 2 "Other")))
(define
  combined
  (content/concat
    (content/prefix-ids d "left-")
    (content/prefix-ids d2 "right-")))
(content-test
  "combined ids unique"
  (doc-tree-ids combined)
  (list "left-h" "left-s" "left-a" "left-b" "right-h"))
(content-test "combined validates" (content/valid? combined) true)
;; without prefixing, the shared id "h" collides
(content-test
  "unprefixed collides"
  (content/valid? (content/concat d d2))
  false)

;; ── render of combined ──
(content-test
  "combined render"
  (asHTML combined)
  "<h1>Title</h1><section><p>A</p><p>B</p></section><h2>Other</h2>")
