#import "variables.typ": *

#let template-shared(content) = [
  // Page style
  #set page(
    "us-trade",
    margin: (
      inside: 0.75in,
      outside: 0.5in,
      top: 0.75in + font-size-small
    )
  )

  // Font style
  #set text(
    size: font-size,
    font: font-body,
  )

  // Paragraph style
  #set par(
    first-line-indent: 1em,
    justify: true
  )

  // Number chapters
  #set heading(
    numbering: (book, ..chapters) => (
      let num = chapters.pos(),
      if num.len() > 0 {
        return "Chapter " + numbering("I.", num.at(0))
      } else {
        return none
      }
    )
  )
  
  #content
]

#let template-chapter-start(content) = [
  // Ignore book headings
  #show heading.where(level:1): it => [
    #return
  ]

  // Chapter heading style
  #show heading.where(level:2): it => [
    // Always start the next chapter on a recto page
    #pagebreak(to: "odd")
    
    #set align(center)
    #let num = " "
    #let has-numbering = it.numbering != none
  
    // Get the chapter number from the heading counter
    #if has-numbering {
      num = counter(heading).display(it.numbering)
    }

    // Chapter number formatting
    #show num: num => {
      set text(
        size: font-size-large,
        weight: "light",
        fill: luma(128)
      )
      smallcaps[#num]
    }
  
    // Space the chapter header from the top of the page
    #pad(
      top: 1in,
      bottom: 1in,
      {
        if has-numbering {
          num
        }
        linebreak()
        set text(font: font-title)
        it.body
      }
    )
  ]
  
  #content
]

#let template-page-header(content) = [
  #set page(
    header: context [
      #let titles = selector(heading.where(level:1))
      #let past-titles = query(titles.before(here()))
      #let chapters = selector(heading.where(level: 2))
      #let past-chapters = query(chapters.before(here()))
      #let this-page = here().page()
      
      // Skip pages before the first chapter, fix the page number
      #let is-before-first-chapter = past-chapters.len() == 0
      #if is-before-first-chapter [
        #counter(page).update(1)
        #return
      ]
      
      // Skip pages where a new chapter starts
      #let future-chapters = query(chapters.after(here()))
      #let is-during-last-chapter = future-chapters.len() == 0
      #if not is-during-last-chapter [
        #let next-chapter-page = future-chapters.first().location().page()
        #let is-chapter-start = next-chapter-page == this-page
        #if is-chapter-start [
          #return
        ]
      ]

      // De-emphasize titles
      #let header-title(content) = {
        set text(
          size: font-size-small,
          weight: "light",
          fill: luma(128)
        )
        smallcaps[#content]
      }

      #set align(horizon)
      #let is-recto = calc.odd(here().page())
      #if is-recto [
        #set align(right)
        #header-title[#past-chapters.last().body]
        #h(1em)
        #counter(page).display()
      ] else [
        #counter(page).display()
        #h(1em)
        #header-title[#past-titles.last().body]
      ]
    ],
    header-ascent: 0%
  )

  #content
]

#let template-outline(content) = [
  #set align(horizon)

  // De-emphasize each chapter number
  #show outline.entry: it => {
    let books = query(selector(heading.where(level: 1))) // I'm so confused why there are nine of  these :(
    show regex("Chapter [^\.]*."): num => [
      #set text(
        size: font-size-small,
        fill: luma(128)
      )
      #smallcaps[#num]
    ]
    it
  }

  #content
]

#let insert(
  content,
  verso-header: ""
) = [
  #show: template-shared
  #show: template-chapter-start
  #show: template-page-header
  
  #content
]


#let insert-outline(
  start-label,
  end-label: <end>
) = [
  #show: template-shared
  #show: template-outline
  #pagebreak()
  #outline(
    title: none,
    target: selector(heading.where(level: 2).after(start-label)).before(end-label)
  )
]
