import { http, HttpResponse } from 'msw'

// OSM/Esri tile stand-in for the fast profile. Returns a tiny transparent
// PNG so `<img>` / Leaflet tile loads succeed in jsdom without exiting the
// process.
const TILE_PNG = Uint8Array.from([
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
  0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x62, 0x00, 0x01, 0x00, 0x00,
  0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
  0x42, 0x60, 0x82,
])

const tile = () => new HttpResponse(TILE_PNG, { headers: { 'Content-Type': 'image/png' } })

export const tilesHandlers = [
  // OSM XYZ scheme: {z}/{x}/{y}
  http.get('https://*.tile.openstreetmap.org/:z/:x/:y.png', tile),
  http.get('https://tile.openstreetmap.org/:z/:x/:y.png', tile),
  // Esri ArcGIS satellite scheme: {z}/{y}/{x}
  http.get('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/:z/:y/:x', tile),
  // Local tile-stub aliases (e2e parity)
  http.get('http://tile-stub:8082/:z/:x/:y.png', tile),
  http.get('http://tile-stub:8082/sat/:z/:y/:x', tile),
  http.get('/tiles/:z/:x/:y.png', tile),
]
