;; content-on-sx — document composition.
;;
;; Combine documents (header + body + footer, templates, partials) into a new
;; document. The result keeps the FIRST document's id and metadata; blocks are
;; concatenated. Immutable — inputs are untouched. Block-id collisions across
;; combined docs are the caller's concern (content/validate flags duplicates).
;;
;; Requires (loaded by harness): doc.sx.

(define
  content/concat
  (fn (a b) (doc-with-blocks a (append (doc-blocks a) (doc-blocks b)))))

(define
  content/prepend
  (fn (a b) (doc-with-blocks a (append (doc-blocks b) (doc-blocks a)))))

(define
  content/-concat-fold
  (fn
    (acc more)
    (if
      (= (len more) 0)
      acc
      (content/-concat-fold (content/concat acc (first more)) (rest more)))))

(define
  content/concat-all
  (fn
    (docs)
    (if
      (= (len docs) 0)
      (doc-empty "merged")
      (content/-concat-fold (first docs) (rest docs)))))

;; wrap a document's blocks inside a single section (collapse to a subtree).
;; Requires section.sx (mk-section) when used.
(define
  content/wrap-section
  (fn
    (doc section-id)
    (doc-with-blocks doc (list (mk-section section-id (doc-blocks doc))))))
