#import "theme.typ": *

#let toc-unnumbered-bodies = ([摘要],)

#let toc-register-chapter-counter() = doc => {
  show heading.where(level: 1): it => {
    if not toc-unnumbered-bodies.contains(it.body) {
      counter("toc-chapter").step()
    }
    it
  }
  doc
}

#let toc-leader(leader-color) = {
  box(
    width: 1fr,
    height: 0.9em,
    clip: true,
    align(horizon + left)[
      #repeat[#text(size: toc-leader-dot-size, fill: leader-color)[·#h(toc-leader-gap)]]
    ],
  )
}

#let toc-format-num(n) = {
  if n < 10 {
    "0" + str(n)
  } else {
    str(n)
  }
}

#let toc-l1-chapter-num(entry) = {
  toc-format-num(counter("toc-chapter").at(entry.element.location()).first())
}

#let toc-number-cell(skip-number, entry) = {
  box(width: toc-l1-number-width)[
    #align(left)[
      #if not skip-number [
        #text(
          font: heading-font,
          size: toc-l1-number-size,
          weight: "bold",
          fill: toc-l1-number-color,
        )[#toc-l1-chapter-num(entry)]
      ]
    ]
  ]
}

#let toc-entry-show(it) = {
  if it.level == 1 and toc-unnumbered-bodies.contains(it.body()) {
    []
  } else if it.level == 1 {
    let skip-number = toc-unnumbered-bodies.contains(it.body())
    block(width: 100%, above: toc-l1-gap-above, below: toc-l1-gap-below)[
      #grid(
        columns: (toc-l1-number-width, toc-l1-number-title-gap, auto, 1fr, auto),
        column-gutter: tw(2),
        align: (left + horizon, left, left + horizon, horizon, horizon),
        toc-number-cell(skip-number, it),
        [],
        text(font: heading-font, size: toc-l1-size, weight: "bold", fill: toc-l1-color)[
          #it.body()
        ],
        toc-leader(toc-l1-leader-color),
        text(font: heading-font, size: toc-page-size, weight: "bold", fill: toc-l1-color)[
          #it.page()
        ],
      )
    ]
  } else if it.level == 2 {
    block(width: 100%, above: toc-l2-gap-above, below: toc-l2-gap-below)[
      #grid(
        columns: (toc-l1-number-width, toc-l1-number-title-gap, auto, 1fr, auto),
        column-gutter: tw(2),
        align: horizon,
        [],
        [],
        text(font: body-font, size: toc-l2-size, weight: "regular", fill: toc-l2-color)[
          #it.body()
        ],
        toc-leader(toc-l2-leader-color),
        text(font: body-font, size: toc-page-size, fill: toc-l2-color)[#it.page()],
      )
    ]
  }
}

#let toc-classification-mark() = {
  place(left + bottom, dx: toc-classification-dx, dy: toc-classification-dy)[
    #text(
      font: body-font,
      size: toc-classification-size,
      fill: toc-classification-color,
      tracking: 0.04em,
    )[
      #rotate(-90deg, reflow: true)[#toc-classification-label]
    ]
  ]
}

#let toc-page(meta) = {
  let _ = meta
  pagebreak(weak: true)

  page(
    header: none,
    footer: none,
    margin: (
      top: page-margin-y,
      bottom: page-margin-y,
      left: page-margin-x,
      right: page-margin-x,
    ),
  )[
    #block(width: 100%, height: 100%)[
      #toc-classification-mark()
      #block(width: 100%, below: space-chapter)[
        #text(font: heading-font, size: toc-head-zh, weight: "bold", fill: toc-head-color)[
          报告目录
        ]

        #v(toc-head-gap)

        #pad(left: toc-content-indent)[
          #show outline.entry: toc-entry-show
          #outline(depth: 2, indent: 0pt, title: none)
        ]
      ]
    ]
  ]

  pagebreak(weak: true)
}
