import { describe, it, expect, vi } from 'vitest'
import { createClusterIndex } from '../../app/utils/mapCluster.js'
import { clearFeatureMarkers, syncFeatureMarkers } from '../../app/utils/mapMarkerSync.js'
import { bboxFetchKey, tilesNearCenter } from '../../app/utils/alprViewport.js'

describe('mapCluster', () => {
  it('loads once and queries by viewport', () => {
    const index = createClusterIndex({ radius: 50, maxZoom: 14, minPoints: 2 })
    const features = Array.from({ length: 20 }, (_, i) => ({
      type: 'Feature',
      geometry: { type: 'Point', coordinates: [-122.4 + i * 0.01, 37.7] },
      properties: { id: i },
    }))
    index.load(features)
    const zoomedOut = index.query({ west: -123, south: 37, east: -122, north: 38, zoom: 6 })
    expect(zoomedOut.some(f => f.properties?.cluster)).toBe(true)
    const zoomedIn = index.query({ west: -123, south: 37, east: -122, north: 38, zoom: 15 })
    expect(zoomedIn).toHaveLength(20)
  })
})

describe('mapMarkerSync', () => {
  it('reuses markers by key', () => {
    const layer = {
      _layers: [],
      addLayer(m) { this._layers.push(m) },
      removeLayer(m) { this._layers = this._layers.filter(x => x !== m) },
    }
    const create = vi.fn(f => ({ id: f.properties.id }))
    const update = vi.fn()
    const opts = { keyFor: f => f.properties.id, create, update }
    const pt = (id, lng, lat) => ({ type: 'Feature', geometry: { type: 'Point', coordinates: [lng, lat] }, properties: { id } })

    syncFeatureMarkers(layer, [pt(1, -122, 37)], opts)
    syncFeatureMarkers(layer, [pt(1, -121.9, 37.1)], opts)
    expect(create).toHaveBeenCalledTimes(1)
    expect(update).toHaveBeenCalledTimes(1)
    clearFeatureMarkers(layer)
    expect(layer._layers).toHaveLength(0)
  })
})

describe('alprViewport', () => {
  it('selects nearby tiles without scanning the world', () => {
    const world = { south: -85, west: -180, north: 85, east: 180 }
    expect(tilesNearCenter(world, 16)).toHaveLength(16)
    expect(tilesNearCenter(world, 1)[0]).toEqual({ south: 0, west: 0, north: 0.5, east: 0.5 })
  })

  it('coarsens fetch keys when zoomed out', () => {
    const bounds = { south: 37.01, west: -122.51, north: 37.99, east: -122.01, zoom: 6 }
    expect(bboxFetchKey(bounds)).toBe(bboxFetchKey({ ...bounds, south: bounds.south + 0.05 }))
    expect(bboxFetchKey({ ...bounds, zoom: 14 })).not.toBe(bboxFetchKey(bounds))
  })
})
