;; Extension — document flatten (un-nest sections).

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

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

;; ── one level un-nested ──
(define f (content/flatten d))
(content-test "flatten ids" (doc-ids f) (list "h" "a" "b"))
(content-test
  "flatten no sections"
  (content/types f)
  (list "heading" "text" "text"))
(content-test "flatten immutable" (doc-ids d) (list "h" "s"))
(content-test "flatten render" (asHTML f) "<h1>Top</h1><p>A</p><p>B</p>")

;; ── deep nesting fully flattened ──
(define
  deep
  (doc-append
    (doc-empty "d")
    (mk-section
      "o"
      (list
        (mk-text "x" "X")
        (mk-section
          "i"
          (list (mk-text "y" "Y") (mk-heading "z" 2 "Z")))))))
(content-test
  "deep flatten ids"
  (doc-ids (content/flatten deep))
  (list "x" "y" "z"))

;; ── inverse of wrap-section ──
(define
  plain
  (doc-append
    (doc-append (doc-empty "p") (mk-text "a" "A"))
    (mk-text "b" "B")))
(content-test
  "flatten . wrap == identity ids"
  (doc-ids (content/flatten (content/wrap-section plain "sec")))
  (doc-ids plain))
(content-test
  "flatten . wrap == identity render"
  (asHTML (content/flatten (content/wrap-section plain "sec")))
  (asHTML plain))

;; ── already-flat doc unchanged ──
(content-test
  "flat unchanged"
  (asHTML (content/flatten plain))
  (asHTML plain))

;; ── empty section disappears ──
(content-test
  "empty section flattens away"
  (doc-ids
    (content/flatten (doc-append (doc-empty "d") (mk-section "s" (list)))))
  (list))

;; ── empty doc ──
(content-test
  "flatten empty"
  (doc-ids (content/flatten (doc-empty "e")))
  (list))
