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

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

#let name = "database"

#let render(x, y, p, bottom) = {
  let m = normalize-measure(p.display-name)
  let style = p.resolved-style
  let width = normalize-units(style.width)
  let spacing = normalize-units(style.spacing)
  let height = width * 4 / 3
  let rx = width / 2
  let ry = rx / 2
  let y0 = if bottom {
    y - m.height - spacing
  } else {
    y + m.height + height + spacing
  }
  let y1 = y0 - height

  draw.merge-path(
    close: true,
    fill: style.fill,
    stroke: style.stroke,
    {
      draw.bezier((x - rx, y0 - ry), (x, y0), (x - rx, y0 - ry/2), (x - rx/2, y0))
      draw.bezier((), (x + rx, y0 - ry), (x + rx/2, y0), (x + rx, y0 - ry/2))
      draw.line((), (x + rx, y1 + ry))
      draw.bezier((), (x, y1), (x + rx, y1 + ry/2), (x + rx/2, y1))
      draw.bezier((), (x - rx, y1 + ry), (x - rx/2, y1), (x - rx, y1 + ry/2))
    }
  )
  draw.merge-path(
    stroke: style.stroke,
    {
      draw.bezier((x - rx, y0 - ry), (x, y0 - ry*2), (x - rx, y0 - 3*ry/2), (x - rx/2, y0 - ry*2))
      draw.bezier((), (x + rx, y0 - ry), (x + rx/2, y0 - ry*2), (x + rx, y0 - 3*ry/2))
    }
  )
  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 width = normalize-units(par.resolved-style.width)
  let height = width * 4 / 3
  let spacing = normalize-units(par.resolved-style.spacing)

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

#let default-style = (
  width: 24pt,
  spacing: 5pt
)