#import "/src/cetz.typ": draw, styles

#import "/src/core/utils.typ": get-ctx, normalize-units, set-ctx

#let delay-default-style = (
  stroke: (
    dash: "loosely-dotted",
    paint: gray.darken(40%),
    thickness: .8pt
  ),
  size: 30pt
)

#let render(delay) = get-ctx(ctx => {
  let style = styles.resolve(
    ctx.style,
    merge: delay.style,
    root: "delay",
    base: delay-default-style
  )
  let size = normalize-units(style.size)

  let y0 = ctx.y
  let y1 = ctx.y - size
  for (i, line) in ctx.lifelines.enumerate() {
    line.events.push((type: "delay-start", y: y0))
    line.events.push((type: "delay-end", y: y1, stroke: style.stroke))
    ctx.lifelines.at(i) = line
  }
  if delay.name != none {
    let x0 = ctx.x-pos.first()
    let x1 = ctx.x-pos.last()
    draw.content(
      ((x0 + x1) / 2, (y0 + y1) / 2),
      anchor: "mid",
      delay.name
    )
  }
  
  set-ctx(c => {
    c.y = y1
    c.lifelines = ctx.lifelines
    return c
  })
})