#import "theme.typ": *
#import "decor.typ": section-tech-id, tech-label

#let underline-accent(body) = underline(
  body,
  stroke: underline-stroke,
  offset: underline-offset,
  evade: true,
)

#let heading-number(it) = {
  if it.numbering != none {
    counter(heading).display(it.numbering)
    h(heading-number-gap)
  }
}

#let report-heading-numbering(..nums) = {
  let n = nums.pos()
  if n.len() == 1 {
    numbering("1.", n.first())
  } else if n.len() == 2 {
    numbering("1.1", n.at(0), n.at(1))
  } else {
    numbering("1.1.1", ..n)
  }
}

#let grelin-h1(it) = block(
  width: 100%,
  above: space-chapter,
  below: space-block,
)[
  #grid(
    columns: (auto, 1fr),
    column-gutter: 8pt,
    align: horizon,
    context section-tech-id(..counter(heading).get()),
    box(width: 100%, fill: color-accent-surface, inset: (x: 8pt, y: 4pt))[
      #text(font: heading-font, size: size-h1, weight: "black", fill: color-ink)[
        #heading-number(it)
        #it.body
      ]
    ],
  )
]

#let grelin-h2(it) = block(width: 100%, above: space-section, below: space-inline)[
  #grid(
    columns: (auto, 1fr),
    column-gutter: 8pt,
    align: horizon,
    box(width: 3pt, height: 1em, fill: color-ink),
    text(font: heading-font, size: size-h2, weight: "bold", fill: color-ink)[
      #heading-number(it)
      #it.body
    ],
  )
]

#let grelin-h3(it) = block(width: 100%, above: space-block, below: space-micro)[
  #grid(
    columns: (auto, 1fr),
    column-gutter: 6pt,
    align: horizon,
    tech-label("///", size: 6.5pt),
    text(font: heading-font, size: size-h3, weight: "bold", fill: color-ink)[
      #heading-number(it)
      #it.body
    ],
  )
]

#let grelin-image(it) = block(
  width: 100%,
  above: space-figure-before,
  below: space-figure-after,
)[
  #align(center)[
    #box(width: image-max-width, stroke: 1pt + color-ink)[
      #set image(width: 100%, fit: "contain")
      #it
    ]
  ]
]

#let grelin-table(it) = block(
  width: 100%,
  above: space-figure-before,
  below: space-figure-after,
)[
  #it
]

#let grelin-figure(it) = block(
  width: 100%,
  above: space-figure-before,
  below: space-figure-after,
)[
  #align(center)[
    #set image(width: image-max-width, fit: "contain")
    #set text(size: size-caption, fill: color-caption)
    #it
  ]
]

#let grelin-blockquote(it) = block(
  width: 100%,
  above: space-block,
  below: space-block,
  inset: (left: blockquote-inset),
  stroke: (left: blockquote-stroke),
  fill: white,
)[
  #set par(leading: leading-body, justify: true)
  #set text(fill: color-quote)
  #it
]

#let grelin-inline-code(it) = box(
  fill: code-inline-fill,
  inset: code-inline-inset,
  stroke: 0.8pt + color-ink,
)[
  #set text(font: mono-font, size: size-body, fill: color-code-inline)
  #it
]

#let apply-report-styling(body) = {
  set heading(numbering: report-heading-numbering)
  set text(size: size-table)
  set table(
    stroke: (x, y) => (
      top: if y == 0 { 1.5pt + color-ink } else { none },
      bottom: if y == 0 { 0.8pt + color-ink } else { none },
    ),
    inset: table-inset,
    align: (col, row) => if col == 0 { left + horizon } else { center + horizon },
    fill: (col, row) => {
      if row == 0 {
        table-header-fill
      } else if calc.rem(row, 2) == 0 {
        table-zebra-fill
      } else {
        white
      }
    },
  )
  show heading.where(level: 1): grelin-h1
  show heading.where(level: 2): grelin-h2
  show heading.where(level: 3): grelin-h3
  show image: grelin-image
  show table.cell: set text(fill: table-data-text)
  show table.cell.where(y: 0): set text(weight: "bold", fill: color-ink)
  show table: grelin-table
  show figure: grelin-figure
  show quote.where(block: true): grelin-blockquote
  show raw.where(block: false): grelin-inline-code
  body
}
