;; Extension — table block.

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

(define nl (str "\n"))

(define
  t
  (mk-table
    "t"
    (list "Name" "Age")
    (list (list "Ada" "36") (list "Al" "40"))))

;; ── identity ──
(content-test "table is block" (block? t) true)
(content-test "table? yes" (table? t) true)
(content-test "table type" (blk-type t) "table")
(content-test "table headers" (table-headers t) (list "Name" "Age"))
(content-test "table rows" (len (table-rows t)) 2)

;; ── html ──
(content-test
  "table html"
  (asHTML t)
  "<table><thead><tr><th>Name</th><th>Age</th></tr></thead><tbody><tr><td>Ada</td><td>36</td></tr><tr><td>Al</td><td>40</td></tr></tbody></table>")
(content-test
  "table html escapes cells"
  (asHTML (mk-table "t" (list "A<B") (list (list "x&y"))))
  "<table><thead><tr><th>A&lt;B</th></tr></thead><tbody><tr><td>x&amp;y</td></tr></tbody></table>")

;; ── sx ──
(content-test
  "table sx"
  (asSx t)
  "(table (thead (tr (th \"Name\")(th \"Age\"))) (tbody (tr (td \"Ada\")(td \"36\"))(tr (td \"Al\")(td \"40\"))))")

;; ── text ──
(content-test "table text" (asText t) "Name Age Ada 36 Al 40")

;; ── markdown ──
(content-test
  "table markdown"
  (asMarkdown t)
  (str "| Name | Age |" nl "| --- | --- |" nl "| Ada | 36 |" nl "| Al | 40 |"))

;; ── in a document ──
(define
  d
  (doc-append
    (doc-append (doc-empty "d") (mk-heading "h" 1 "Data"))
    t))
(content-test
  "doc with table html"
  (asHTML d)
  "<h1>Data</h1><table><thead><tr><th>Name</th><th>Age</th></tr></thead><tbody><tr><td>Ada</td><td>36</td></tr><tr><td>Al</td><td>40</td></tr></tbody></table>")
(content-test "doc ids" (doc-ids d) (list "h" "t"))

;; ── empty rows ──
(content-test
  "table no rows html"
  (asHTML (mk-table "t" (list "H") (list)))
  "<table><thead><tr><th>H</th></tr></thead><tbody></tbody></table>")

;; ── validation ──
(content-test
  "valid table"
  (content/valid? (doc-append (doc-empty "d") t))
  true)
(content-test
  "bad headers flagged"
  (content/issue-kinds
    (doc-append (doc-empty "d") (mk-table "t" "nope" (list))))
  (list "field"))
