import { useMapEditorComposable } from '@/composables/useMapEditorComposable'
import { computed, watch, type Ref } from 'vue'
import { useGamePointerHandlers } from './pointerHandlers/useGamePointerHandlers'
import { useMapEditorPointerHandlers } from './pointerHandlers/useMapEditorPointerHandlers'

export function usePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilemaps.TilemapLayer, waypoint: Ref<{ visible: boolean; x: number; y: number }>, camera: Phaser.Cameras.Scene2D.Camera) {
  const mapEditor = useMapEditorComposable()
  const gameHandlers = useGamePointerHandlers(scene, layer, waypoint, camera)
  const mapEditorHandlers = useMapEditorPointerHandlers(scene, layer, waypoint, camera)

  const currentHandlers = computed(() => (mapEditor.active.value ? mapEditorHandlers : gameHandlers))

  const setupPointerHandlers = () => currentHandlers.value.setupPointerHandlers()
  const cleanupPointerHandlers = () => currentHandlers.value.cleanupPointerHandlers()

  watch(
    () => mapEditor.active.value,
    () => {
      cleanupPointerHandlers()
      setupPointerHandlers()
    }
  )

  return { setupPointerHandlers, cleanupPointerHandlers }
}
