#let months = yaml("../data/months.yaml")

#let getWithLang(lang, data) = {
  if type(data) == dictionary {
    data.at(lang)
  } else {
    data
  }
}

// Helper Functions
#let monthname(lang, n, display: "short") = {
  n = int(n)
  let month = ""

  let displayMonths = getWithLang(lang, months.at(display))

  if (n > 0 and n <= 12) {
    month = displayMonths.at(n - 1)
  }

  month
}

#let strpdate(lang, isodate) = {
  let date = ""
  let isodate = getWithLang(lang, isodate)
  if (regex("\d{4}-\d{2}-\d{2}") in isodate) {
    date = datetime(
      year: int(isodate.slice(0, 4)),
      month: int(isodate.slice(5, 7)),
      day: int(isodate.slice(8, 10)),
    )
    date = monthname(lang, date.month()) + " " + date.display("[year repr:full]")
  } else {
    date = isodate
  }
  return date
}

#let filterLanguage(lang, data) = {
  if type(data) == dictionary {
    if data.at(lang, default: none) == none {
      data = data
      .pairs()
      .map(xs => (xs.first(), filterLanguage(lang, xs.last())))
      .fold((:), (acc, xs) => {
        acc.insert(xs.first(), xs.last())
        acc
      })
    } else {
      data = filterLanguage(lang, data.at(lang))
    }
  } else if type(data) == array {
    data = data.map(v => filterLanguage(lang, v))
  }

  data
}