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

#import "/src/core/utils.typ": normalize-measure, normalize-units

#let name = "boundary"

#let render(x, y, p, bottom) = {
  let m = normalize-measure(p.display-name)
  let style = p.resolved-style
  let height = normalize-units(style.height)
  let spacing = normalize-units(style.spacing)
  let circle-r = height / 2
  let y0 = if bottom {
    y - m.height - spacing
  } else {
    y + m.height + height + spacing
  }
  let x0 = x - height
  let y1 = y0 - circle-r
  let y2 = y0 - height

  draw.circle(
    (x + circle-r, y1),
    radius: circle-r,
    fill: style.fill,
    stroke: style.stroke
  )
  draw.line(
    (x0, y0), (x0, y2),
    stroke: style.stroke
  )
  draw.line(
    (x0, y1), (x, y1),
    stroke: style.stroke
  )
  draw.content(
    (x, y),
    p.display-name,
    anchor: if bottom {"north"} else {"base"}
  )
}

#let get-size(par) = {
  let m = normalize-measure(par.display-name)

  let height = normalize-units(par.resolved-style.height)
  let width = height * 2
  let spacing = normalize-units(par.resolved-style.spacing)

  return (
    width: calc.max(m.width, width),
    height: height + spacing + m.height
  )
}

#let default-style = (
  height: 20pt,
  spacing: 5pt
)