/// Typst handout library, used for all documents in this repository.

// Re-exports
// All functions that maybe used by client code are listed here
#import "misc.typ": *
#import "object.typ": problem, definition, theorem
#import "solution.typ": if_solutions, if_no_solutions, solution


/// Main handout wrapper.
/// Use this as follows:
///
/// ```
/// #show: handout.with(
///  group: "Advanced 2",
///  title: [Handout Title],
///  by: "author",
///  subtitle: "optional",
/// )
///
/// <rest of document>
/// ```
#let handout(
  doc,
  group: none,
  title: none,
  by: none,
  subtitle: none,
) = {
  set page(
    margin: 20mm,
    width: 8.5in,
    height: 11in,
    footer: align(
      center,
      context counter(page).display(),
    ),
    footer-descent: 5mm,
  )

  //
  // Text style
  set text(font: "New Computer Modern")
  set par(
    leading: 0.55em,
    first-line-indent: 0mm,
    justify: true,
    spacing: 0.5em,
  )

  //
  // List style
  show list: set block(spacing: 0.5em, below: 1em)
  set list(
    tight: false,
    indent: 5mm,
    spacing: 3mm,
  )

  //
  // Heading style
  set heading(numbering: (..nums) => nums.pos().at(0))
  show heading.where(level: 1): it => {
    set align(center)
    set text(weight: "bold")
    block[
      Section #counter(heading).display(): #text(it.body)
    ]
  }

  //
  // Hack for custom references
  show ref: it => {
    import "object.typ": ref_obj

    let x = ref_obj(it) // Custom impl for object references
    if (x != none) { return x }

    return it // Use default `ref` implementation otherwise
  }


  //
  // Begin content
  //

  // Make handout title
  {
    import "header.typ": make_header, solution_warning
    import "solution.typ": show_solutions

    let url = link(
      "https://betalupi.com/handouts",
      `betalupi.com/handouts`,
    )

    make_header(
      title,
      subtitle: subtitle,
      by: by,
      top_left: group,
      top_right: url,
    )

    if show_solutions {
      solution_warning()
    }
  }

  // Include rest of document
  doc
}

