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

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

#let name = "collections"

#let render(x, y, p, bottom) = {
  let m = measure(p.display-name)
  let style = p.resolved-style
  let w = m.width / 1pt
  let h = m.height / 1pt
  let dx = COLLECTIONS-DX
  let dy = COLLECTIONS-DY
  let total-w = w + PAR-PAD.last() * 2 / 1pt + calc.abs(dx)
  let total-h = h + PAR-PAD.first() * 2 / 1pt + calc.abs(dy)

  let x0 = x - total-w / 2
  let x1 = x0 + calc.abs(dx)
  let x3 = x0 + total-w
  let x2 = x3 - calc.abs(dx)
  
  let y0 = if bottom {y} else {y + total-h}
  let y1 = y0 - calc.abs(dy)
  let y3 = y0 - total-h
  let y2 = y3 + calc.abs(dy)

  let r1 = (x1, y0, x3, y2)
  let r2 = (x0, y1, x2, y3)

  if dx < 0 {
    r1.at(0) = x0
    r1.at(2) = x2
    r2.at(0) = x1
    r2.at(2) = x3
  }

  if dy < 0 {
    r1.at(1) = y1
    r1.at(3) = y3
    r2.at(1) = y0
    r2.at(3) = y2
  }
  draw.rect(
    (r1.at(0), r1.at(1)),
    (r1.at(2), r1.at(3)),
    fill: style.fill,
    stroke: style.stroke
  )
  draw.rect(
    (r2.at(0), r2.at(1)),
    (r2.at(2), r2.at(3)),
    fill: style.fill,
    stroke: style.stroke
  )
  
  draw.content(
    ((r2.at(0) + r2.at(2)) / 2, (r2.at(1) + r2.at(3)) / 2),
    p.display-name,
    anchor: "mid"
  )
}

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

  // w + COLLECTIONS-PAD.last() * 2 + calc.abs(COLLECTIONS-DX) * 1pt
  // h + COLLECTIONS-PAD.first() * 2 + calc.abs(COLLECTIONS-DY) * 1pt

  return m
}

#let default-style = (
  :
)