;; Extension — nested document outline.

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

;; H1 / H2 H2 / H1  ->  [h1{children: h2,h3}, h4]
(define
  d
  (doc-append
    (doc-append
      (doc-append
        (doc-append (doc-empty "d") (mk-heading "a" 1 "A"))
        (mk-heading "b" 2 "B"))
      (mk-heading "c" 2 "C"))
    (mk-heading "e" 1 "E")))

(define o (content/outline d))
(content-test "outline top count" (len o) 2)
(content-test "outline first id" (get (first o) :id) "a")
(content-test
  "outline first children ids"
  (map (fn (n) (get n :id)) (get (first o) :children))
  (list "b" "c"))
(content-test "outline second top" (get (nth o 1) :id) "e")
(content-test
  "outline second no children"
  (get (nth o 1) :children)
  (list))

;; ── deeper nesting: H1 / H2 / H3 ──
(define
  d2
  (doc-append
    (doc-append
      (doc-append (doc-empty "d") (mk-heading "x" 1 "X"))
      (mk-heading "y" 2 "Y"))
    (mk-heading "z" 3 "Z")))
(define o2 (content/outline d2))
(content-test "deep top" (get (first o2) :id) "x")
(content-test
  "deep child"
  (get (first (get (first o2) :children)) :id)
  "y")
(content-test
  "deep grandchild"
  (get (first (get (first (get (first o2) :children)) :children)) :id)
  "z")

;; ── node carries text + level ──
(content-test "node text" (get (first o) :text) "A")
(content-test "node level" (get (first o) :level) 1)

;; ── empty / no headings ──
(content-test "outline empty" (content/outline (doc-empty "e")) (list))
(content-test
  "outline no headings"
  (content/outline (doc-append (doc-empty "d") (mk-text "p" "x")))
  (list))

;; ── starting at H2 (no H1) still forms a forest ──
(define
  d3
  (doc-append
    (doc-append (doc-empty "d") (mk-heading "p" 2 "P"))
    (mk-heading "q" 2 "Q")))
(content-test "no-h1 forest count" (len (content/outline d3)) 2)

;; ── headings nested inside sections are found (tree-wide via query) ──
(define
  d4
  (doc-append
    (doc-append (doc-empty "d") (mk-heading "top" 1 "Top"))
    (mk-section "s" (list (mk-heading "in" 2 "In")))))
(content-test
  "section heading nested in outline"
  (map (fn (n) (get n :id)) (get (first (content/outline d4)) :children))
  (list "in"))
