;; Extension — nested block trees (CtSection container).

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

(define nl (str "\n"))

;; ── a section is a block ──
(define
  sec
  (mk-section
    "s"
    (list (mk-heading "h" 2 "Hi") (mk-text "p" "Body"))))
(content-test "section is block" (block? sec) true)
(content-test "section? yes" (section? sec) true)
(content-test "section? no on text" (section? (mk-text "x" "y")) false)
(content-test "section type" (blk-type sec) "section")
(content-test "section id" (blk-id sec) "s")
(content-test
  "section children count"
  (len (section-children sec))
  2)

;; ── recursive render ──
(content-test
  "section html"
  (asHTML sec)
  "<section><h2>Hi</h2><p>Body</p></section>")
(content-test "section sx" (asSx sec) "(section (h2 \"Hi\")(p \"Body\"))")
(content-test "section text" (asText sec) "Hi Body")
(content-test
  "empty section html"
  (asHTML (mk-section "e" (list)))
  "<section></section>")

;; ── nested in a document ──
(define
  d
  (doc-append
    (doc-append (doc-empty "d") (mk-heading "top" 1 "Top"))
    sec))
(content-test
  "doc with section html"
  (asHTML d)
  "<h1>Top</h1><section><h2>Hi</h2><p>Body</p></section>")
(content-test "doc top-level ids" (doc-ids d) (list "top" "s"))

;; ── arbitrary depth ──
(define
  deep
  (mk-section
    "outer"
    (list
      (mk-text "a" "A")
      (mk-section
        "inner"
        (list (mk-text "b" "B") (mk-heading "c" 3 "C"))))))
(content-test
  "deep html"
  (asHTML deep)
  "<section><p>A</p><section><p>B</p><h3>C</h3></section></section>")
(content-test "deep text" (asText deep) "A B C")

;; ── tree traversal descends into sections ──
(define dd (doc-append (doc-empty "d") deep))
(content-test "deep-find nested" (blk-id (doc-deep-find dd "b")) "b")
(content-test
  "deep-find deeper"
  (str (blk-send (doc-deep-find dd "c") "text"))
  "C")
(content-test "deep-find missing" (doc-deep-find dd "zzz") nil)
(content-test
  "deep-find top-level"
  (blk-id (doc-deep-find dd "outer"))
  "outer")
(content-test
  "tree-ids flattened"
  (doc-tree-ids dd)
  (list "outer" "a" "inner" "b" "c"))
(content-test "tree-count" (doc-tree-count dd) 5)
(content-test "top-level ids still flat" (doc-ids dd) (list "outer"))

;; ── copy-on-write child edits ──
(define sec2 (section-append sec (mk-divider "dv")))
(content-test "section-append" (len (section-children sec2)) 3)
(content-test
  "section-append immutable"
  (len (section-children sec))
  2)
(content-test
  "section-append renders"
  (asHTML sec2)
  "<section><h2>Hi</h2><p>Body</p><hr></section>")

;; ── markdown of a section (children joined by blank line) ──
(content-test "section markdown" (asMarkdown sec) (str "## Hi" nl nl "Body"))
