#import "../utils.typ": get-ctx, set-ctx, is-elmt

#let render(sync) = get-ctx(ctx => {
  set-ctx(c => {
    c.sync-ys = ()
    return c
  })

  for e in sync.elmts {
    assert(is-elmt(e), message: "Sync element can only contain chronos elements, found " + repr(e))
    assert(
      e.type == "seq",
      message: "Sync element can only contain sequences, found '" + e.type + "'"
    )

    set-ctx(c => {
      c.y = ctx.y
      return c
    })
    (e.draw)(e)
    set-ctx(c => {
      c.sync-ys.push(c.y)
      return c
    })
  }

  set-ctx(c => {
    c.y = calc.min(..c.sync-ys)
    c.remove("sync-ys")
    return c
  })
})