#import "etablissements.typ": etablissements-data
#import "matisse-thesis-var.typ": school-color-verso
#let assets-folder = "../assets/"


// workaround for: https://github.com/typst/typst/issues/466
#let balanced-cols(n-cols, gutter: 11pt, body) = layout(bounds => context {
  // Measure the height of the container of the text if it was single
  // column, full width
  let text-height = measure(box(
    width: (bounds.width - (n-cols - 1) *  gutter) / n-cols,
    body
  )).height

  // Recompute the height of the new container. Add a few points to avoid the
  // second column being longer than the first one
  let balanced-height = text-height/n-cols + text.size/2

  box(
    height: balanced-height,
    columns(n-cols, gutter: gutter, body)
  )
})

#let abstracts(
  university: "UR",
  title-fr: "",
  keywords-fr: (),
  abstract-fr: [],
  title-en: "",
  keywords-en: (),
  abstract-en: [],
  font: "TeX Gyre Heros",
  font-size: 11pt,
  school-color-verso: school-color-verso,
  align-to-even-page: true,
) = {
  set page(
    numbering: none,
    header: none,
  )

  if align-to-even-page {
    pagebreak()
    pagebreak(to: "even")
  }

  set page(
    margin: (
      left: 20mm, right: 30mm,
      top: 30mm, bottom: 30mm,
    ),
    background: image(assets-folder + "abstracts-bg.svg")
  )
  set text(font: font, fill: black, size: font-size)

  place(dx: 100mm, dy: -15mm, etablissements-data.at(university).logo)
  place(dx: 0mm, dy: -15mm, image(assets-folder + "logo.png", width: 7.5cm))

  v(2cm)
  line(length: 100%, stroke: .2cm + school-color-verso)
  v(.4cm)

  [
    #show linebreak: none
    #text(school-color-verso)[Titre :] #title-fr
  ]

  [

    Mots clés : #keywords-fr.join(", ")
  ]

  balanced-cols(2,gutter: 11pt)[Résumé : #abstract-fr]

  v(1cm)
  line(length: 100%, stroke: .2cm + school-color-verso)
  v(.4cm)

  [
    #show linebreak: none
    #text(school-color-verso)[Title :] #title-en
  ]

  [

    Keywords: #keywords-en.join(", ")
  ]

  balanced-cols(2, gutter: 11pt)[Abstract: #abstract-en]
}
