{
  "version": 3,
  "sources": ["map_3d_terrain.js"],
  "sourcesContent": ["// @ts-check\n/*\n * static/js/map_3d_terrain.js \u2014 MapLibre terrain, installed on a map\n * (SNOW-1449).\n *\n * Installed by /map/'s 3D view (map_view_3d.js); SNOW-1450 retired the\n * /map/3d/ sandbox that shared it, so one map now draws the relief\n * (docs/decisions/a-3d-map-is-maplibre-terrain.md). What it owns:\n *\n *   - THE ELEVATION PROTOCOL. `map3ddem://` answered by map_3d_core's\n *     loader: Mapterhorn (or our copy of it) first, AWS's Terrarium tiles\n *     when it fails, an AWS tile enlarged nearest-neighbour to 512 px.\n *     Registered once per page whoever asks: MapLibre keeps one handler\n *     per protocol name.\n *   - THE TERRAIN. The elevation source, setTerrain, the sky and the\n *     north-west sunlight hillshade under the style's first label layer.\n *     installTerrain is idempotent, so a caller can run it after every\n *     setStyle without counting; removeTerrain takes the relief and the\n *     sunlight away and keeps the source, so a return costs nothing.\n *   - THE PREFETCH. The elevation tiles a 3D view around a camera asks\n *     for first, fetched into the browser's cache once per view, so the\n *     relief is there when 3D opens (the 3D roundel).\n *\n * Exports (frozen `self.pwaMap3dTerrain`):\n *\n *   SUNLIGHT_LAYER\n *   registerDemProtocol(maplibre, demTileUrl)\n *   installTerrain(map)\n *   removeTerrain(map)\n *   hasTerrain(map)\n *   createPrefetcher(template)\n */\n(function () {\n  'use strict';\n\n  const core = self.pwaMap3dCore;\n  if (!core) return;\n\n  /** The sunlight hillshade's layer id. */\n  const SUNLIGHT_LAYER = 'map-3d-sunlight';\n\n  /** @type {WeakSet<object>} The MapLibre namespaces the protocol is on. */\n  const registered = new WeakSet();\n\n  /**\n   * Enlarge a 256 px AWS tile to the 512 px the source declares, without\n   * blending: Terrarium heights are encoded across the three channels.\n   *\n   * @param {Blob} blob\n   * @returns {Promise<ImageBitmap>}\n   */\n  async function enlarge(blob) {\n    const size = core.DEM_TILE_SIZE;\n    const image = await createImageBitmap(blob);\n    const canvas = document.createElement('canvas');\n    canvas.width = size;\n    canvas.height = size;\n    const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));\n    ctx.imageSmoothingEnabled = false;\n    ctx.drawImage(image, 0, 0, size, size);\n    image.close();\n    return createImageBitmap(canvas);\n  }\n\n  /**\n   * Answer `map3ddem://` with Mapterhorn's tiles, AWS's when they fail.\n   * Both are fetched without credentials, as the prefetch fetches them, so\n   * a prefetched tile is answered from the browser's cache. A second call\n   * for the same MapLibre does nothing.\n   *\n   * @param {any} maplibre The `maplibregl` namespace.\n   * @param {string} demTileUrl The primary tiles' XYZ template.\n   * @returns {boolean} Whether this call registered it.\n   */\n  function registerDemProtocol(maplibre, demTileUrl) {\n    if (!maplibre || registered.has(maplibre)) return false;\n    registered.add(maplibre);\n    const loader = core.createDemLoader({\n      primary: demTileUrl || '',\n      fallback: core.DEM_FALLBACK_TILE_URL,\n      fetchTile: (/** @type {string} */ url, /** @type {AbortSignal} */ signal) =>\n        fetch(url, { signal, credentials: 'omit' }),\n      enlarge,\n    });\n    maplibre.addProtocol(\n      core.DEM_PROTOCOL,\n      /**\n       * @param {{url: string}} params\n       * @param {AbortController} abortController\n       */\n      (params, abortController) => {\n        const tile = core.parseDemUrl(params.url);\n        if (!tile) return Promise.reject(new Error(`map_3d: not an elevation url: ${params.url}`));\n        return loader.load(tile, abortController.signal);\n      },\n    );\n    return true;\n  }\n\n  /**\n   * Whether the map is drawing the relief now.\n   *\n   * @param {any} map\n   * @returns {boolean}\n   */\n  function hasTerrain(map) {\n    const terrain = typeof map.getTerrain === 'function' ? map.getTerrain() : null;\n    return !!terrain && terrain.source === core.DEM_SOURCE_ID;\n  }\n\n  /**\n   * Lift the map into relief: the elevation source, the terrain, the sky\n   * and the sunlight. Each part is added only if missing, so a second call,\n   * or one after a setStyle wiped some of it, puts back exactly what is\n   * gone.\n   *\n   * @param {any} map\n   * @returns {void}\n   */\n  function installTerrain(map) {\n    if (!map.getSource(core.DEM_SOURCE_ID)) map.addSource(core.DEM_SOURCE_ID, core.demSource());\n    if (!hasTerrain(map)) map.setTerrain(core.terrainSpec());\n    map.setSky(core.skySpec());\n    if (!map.getLayer(SUNLIGHT_LAYER)) {\n      // Under the basemap's labels and over its hachures, so the relief\n      // reads as form. It shades from the terrain's own elevation source,\n      // so it downloads nothing more.\n      const style = map.getStyle();\n      const layers = (style && style.layers) || [];\n      const firstLabel = layers.find((/** @type {any} */ l) => l.type === 'symbol');\n      map.addLayer(\n        { id: SUNLIGHT_LAYER, type: 'hillshade', source: core.DEM_SOURCE_ID, paint: core.sunlightPaint() },\n        firstLabel ? firstLabel.id : undefined,\n      );\n    }\n  }\n\n  /**\n   * Lay the map flat again: no relief and no sunlight. The elevation\n   * source stays, so coming back to 3-D re-reads nothing.\n   *\n   * @param {any} map\n   * @returns {void}\n   */\n  function removeTerrain(map) {\n    if (hasTerrain(map)) map.setTerrain(null);\n    if (map.getLayer(SUNLIGHT_LAYER)) map.removeLayer(SUNLIGHT_LAYER);\n  }\n\n  /**\n   * A prefetch of the elevation tiles a 3-D view around a camera asks for\n   * first (map_3d_core.prefetchDemTiles). Once per view: a call for the\n   * same tiles as the last one fetches nothing.\n   *\n   * @param {string} template The primary tiles' XYZ template.\n   * @returns {(centre: {lng: number, lat: number}, zoom: number) => number} The number of tiles fetched.\n   */\n  function createPrefetcher(template) {\n    let last = '';\n    return (centre, zoom) => {\n      if (!template) return 0;\n      const tiles = core.prefetchDemTiles(centre, zoom);\n      const key = tiles.map((/** @type {{z: number, x: number, y: number}} */ t) => `${t.z}/${t.x}/${t.y}`).join(' ');\n      if (key === last) return 0;\n      last = key;\n      for (const tile of tiles) {\n        fetch(core.tileUrl(template, tile), { credentials: 'omit' }).catch(() => {});\n      }\n      return tiles.length;\n    };\n  }\n\n  self.pwaMap3dTerrain = Object.freeze({\n    SUNLIGHT_LAYER,\n    registerDemProtocol,\n    installTerrain,\n    removeTerrain,\n    hasTerrain,\n    createPrefetcher,\n  });\n})();\n"],
  "mappings": "CAgCC,UAAY,CACX,aAEA,MAAM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EACrFkB,EAAWC,CAAG,GAAGA,EAAI,WAAWnB,EAAK,YAAY,CAAC,EACvDmB,EAAI,OAAOnB,EAAK,QAAQ,CAAC,EACrB,CAACmB,EAAI,SAASlB,CAAc,EAAG,CAIjC,MAAMqB,EAAQH,EAAI,SAAS,EAErBI,GADUD,GAASA,EAAM,QAAW,CAAC,GACjB,KAAyBE,GAAMA,EAAE,OAAS,QAAQ,EAC5EL,EAAI,SACF,CAAE,GAAIlB,EAAgB,KAAM,YAAa,OAAQD,EAAK,cAAe,MAAOA,EAAK,cAAc,CAAE,EACjGuB,EAAaA,EAAW,GAAK,MAC/B,CACF,CACF,CASA,SAASE,EAAcN,EAAK,CACtBD,EAAWC,CAAG,GAAGA,EAAI,WAAW,IAAI,EACpCA,EAAI,SAASlB,CAAc,GAAGkB,EAAI,YAAYlB,CAAc,CAClE,CAUA,SAASyB,EAAiBC,EAAU,CAClC,IAAIC,EAAO,GACX,MAAO,CAACC,EAAQC,IAAS,CACvB,GAAI,CAACH,EAAU,MAAO,GACtB,MAAMI,EAAQ/B,EAAK,iBAAiB6B,EAAQC,CAAI,EAC1CE,EAAMD,EAAM,IAAsDE,GAAM,GAAGA,EAAE,CAAC,IAAIA,EAAE,CAAC,IAAIA,EAAE,CAAC,EAAE,EAAE,KAAK,GAAG,EAC9G,GAAID,IAAQJ,EAAM,MAAO,GACzBA,EAAOI,EACP,UAAWf,KAAQc,EACjB,MAAM/B,EAAK,QAAQ2B,EAAUV,CAAI,EAAG,CAAE,YAAa,MAAO,CAAC,EAAE,MAAM,IAAM,CAAC,CAAC,EAE7E,OAAOc,EAAM,MACf,CACF,CAEA,KAAK,gBAAkB,OAAO,OAAO,CACnC,eAAA9B,EACA,oBAAAQ,EACA,eAAAY,EACA,cAAAI,EACA,WAAAP,EACA,iBAAAQ,CACF,CAAC,CACH,GAAG",
  "names": ["core", "SUNLIGHT_LAYER", "registered", "enlarge", "blob", "size", "image", "canvas", "ctx", "registerDemProtocol", "maplibre", "demTileUrl", "loader", "url", "signal", "params", "abortController", "tile", "hasTerrain", "map", "terrain", "installTerrain", "style", "firstLabel", "l", "removeTerrain", "createPrefetcher", "template", "last", "centre", "zoom", "tiles", "key", "t"]
}
