import { http, HttpResponse } from 'msw'

// Satellite-provider tile stand-in for the fast profile (AZ-498).
// Returns a tiny transparent PNG so `<img>` / Leaflet tile loads succeed in
// jsdom without exiting the process.
//
// The contract `_docs/02_document/contracts/satellite-provider/tiles.md`
// (v1.0.0) freezes the path shape `/tiles/{z}/{x}/{y}` (no `.png` suffix,
// `image/jpeg` Content-Type, cookie auth on the same origin). The handler
// matches that exact shape and the dev default URL the SPA falls back to.
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/jpeg',
      'Cache-Control': 'public, max-age=86400',
      'ETag': '"fast-profile-fixture"',
    },
  })

export const tilesHandlers = [
  http.get('/tiles/:z/:x/:y', tile),
  http.get('http://localhost:5100/tiles/:z/:x/:y', tile),
  http.get('http://tile-stub:8082/tiles/:z/:x/:y', tile),
]
