import { useDominoStore } from './domino-hooks'

export function useTransform() {
  const viewport = useDominoStore(state => state.viewport)

  const screenToWorld = (
    clientX: number,
    clientY: number,
    containerRect?: DOMRect,
  ) => {
    const x = containerRect ? clientX - containerRect.left : clientX
    const y = containerRect ? clientY - containerRect.top : clientY
    return {
      x: (x - viewport.x) / viewport.scale,
      y: (y - viewport.y) / viewport.scale,
    }
  }

  const worldToScreen = (
    worldX: number,
    worldY: number,
    containerRect?: DOMRect,
  ) => {
    const x = worldX * viewport.scale + viewport.x
    const y = worldY * viewport.scale + viewport.y
    return {
      x: containerRect ? x + containerRect.left : x,
      y: containerRect ? y + containerRect.top : y,
    }
  }

  return { screenToWorld, worldToScreen }
}
