import maplibregl from 'maplibre-gl'
import { Protocol } from 'pmtiles'

/**
 * The app's single `pmtiles://` protocol handler.
 *
 * MapLibre keeps one handler per scheme globally, so a second
 * `addProtocol('pmtiles', …)` replaces the first. That matters because the
 * main map registers IndexedDB-backed archives on its instance via
 * `protocol.add()` — if another map registered its own instance, every
 * downloaded region would stop resolving. Sharing one instance means URL-
 * backed sources (which `Protocol` builds on demand) and IndexedDB-backed
 * ones can coexist on the same page.
 */
export const protocol = new Protocol()

maplibregl.addProtocol('pmtiles', protocol.tile.bind(protocol))
