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

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

#let name = "database"

#let render(x, y, p, bottom) = {
  let m = measure(p.display-name)
  let style = p.resolved-style
  let height = DATABASE-WIDTH * 4 / 3
  let rx = DATABASE-WIDTH / 2
  let ry = rx / 2
  let y0 = if bottom {
    y - m.height / 1pt - SYM-GAP
  } else {
    y + m.height / 1pt + height + SYM-GAP
  }
  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)

  // DATABASE-WIDTH * 1pt
  // DATABASE-WIDTH * 4pt / 3 + SYM-GAP * 1pt + h

  return m
}

#let default-style = (
  :
)