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

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

#let name = "queue"

#let render(x, y, p, bottom) = {
  let style = p.resolved-style
  let m = normalize-measure(box(
    p.display-name,
    inset: style.inset
  ))
  
  let w = m.width
  let h = m.height
  let ry = h / 2
  let rx = ry / 2
  let total-w = w + 3 * rx

  let y0 = if bottom {y} else {y + h}
  let y1 = y0 - h
  let ym = y0 - ry

  let xll = x - total-w / 2
  let xrr = x + total-w / 2
  let xlm = xll + rx
  let xrm = xrr - rx
  let xrl = xrm - rx
  
  /*
   /A----------/B\   --- y0
   Fh          G C   --- ym
   \E----------\D/   --- y1
  
   ||          |||
   ||          ||\-- xrr
   ||          |\--- xrm
   ||          \---- xrl
   |\--------------- xlm
   \---------------- xll
  
  h <-> G == w
  F <-> h == rx
  G <-> C == 2 * rx == ry
  A <-> E == B <-> D == 2 * ry == h
  */

  draw.merge-path(
    close: true,
    fill: style.fill,
    stroke: style.stroke,
    {
      draw.bezier(
        (xrm, y0),
        (xrr, ym),
        (xrm + rx/2, y0),
        (xrr, ym + ry/2)
      )
      draw.bezier(
        (),
        (xrm, y1),
        (xrr, ym - ry/2),
        (xrm + rx/2, y1)
      )
      draw.line((), (xlm, y1))
      draw.bezier(
        (),
        (xll, ym),
        (xlm - rx/2, y1),
        (xll, ym - ry/2)
      )
      draw.bezier(
        (),
        (xlm, y0),
        (xll, ym + ry/2),
        (xlm - rx/2, y0)
      )
    }
  )
  draw.merge-path(
    stroke: style.stroke,
    {
      draw.bezier(
        (xrm, y0),
        (xrl, ym),
        (xrm - rx/2, y0),
        (xrl, ym + ry/2)
      )
      draw.bezier(
        (),
        (xrm, y1),
        (xrl, ym - ry/2),
        (xrm - rx/2, y1)
      )
    }
  )
  draw.content(
    ((xlm + xrl) / 2, ym),
    p.display-name,
    anchor: "mid"
  )
}

#let get-size(par) = {
  let m = normalize-measure(box(
    par.display-name,
    inset: par.resolved-style.inset
  ))

  let rx = m.height / 4

  return (
    width: m.width + 3 * rx,
    height: m.height
  )
}

#let default-style = (
  inset: (x: 3pt, y: 5pt)
)