// Routes native physics contact/sensor events to the owning nodes' 'enter'/'exit' events. Each event
// is delivered to BOTH nodes with the other as the argument. type: 0 = enter (contact/overlap began),
// 1 = exit (ended). Registered lazily on the first trigger/physics body or 'enter'/'exit' listener.

import { nodeRegistry } from "./Node"

let registered = false
export const ensurePhysicsEvents = (): void => {
  if (registered) return
  if (!_creator.setOnPhysicsEvent) return // physics-less build / bridge without the hook
  registered = true
  _creator.setOnPhysicsEvent((a, b, type) => {
    const na = nodeRegistry.get(a)
    const nb = nodeRegistry.get(b)
    if (!na || !nb) return
    const channel = type === 1 ? "exit" : "enter"
    na._emitCollision(channel, nb)
    nb._emitCollision(channel, na)
  })
}
