{
  "version": 3,
  "sources": ["basemap_area_removal.js"],
  "sourcesContent": ["/*\n * static/js/basemap_area_removal.js \u2014 remove downloaded areas from this\n * device: the one implementation (SNOW-1308).\n *\n * Until SNOW-1308 this was `evictBasemapAreas` inside\n * `map_basemap_downloads.js`, which only the map loads. The `/offline/`\n * panels needed the same Remove \u2014 for a recorded area and for an orphaned\n * bucket no record accounts for (SNOW-1138) \u2014 and a second copy of the\n * removal on that page would be two answers to \"what does Remove delete\"\n * that could drift. So it moved here, and both surfaces load this file:\n *\n *   - the map's Manage downloads sheet and its budget eviction, through\n *     `map_basemap_downloads.js`'s `evictBasemapAreas`, which keeps the two\n *     map-only follow-ups (the downloaded-areas overlay and the\n *     `snowdesk:downloads-changed` event) and delegates the rest here;\n *   - the `/offline/` panels (`offline_journey.js`), which re-read storage\n *     through the audit afterwards rather than trusting this to have\n *     worked.\n *\n * What one removal does, best-effort per area so one failure never aborts\n * the rest:\n *\n *   1. deletes each area's pinned Cache Storage bucket;\n *   2. removes its record from `basemap.customAreas` or `basemap.regions`\n *      where one exists \u2014 an orphaned bucket has none, and is simply\n *      deleted;\n *   3. tells the service worker its memoised pinned-bucket list is stale\n *      (SNOW-613), or the worker would recreate the deleted bucket empty.\n *\n * The record writes are one read and one write per record TYPE, never one\n * per id: both rows are whole arrays, and a per-id read-filter-write run in\n * parallel is a lost update the moment two ids of one type go in one call\n * (SNOW-635 review).\n *\n * Needs `pwaBasemapDownloadCore` (the bucket-id family) and `pwaDb`;\n * `pwaBasemapAreas` for the custom-area row, whose reader owns the legacy\n * migration. `available()` says whether they are all here, so a host that\n * lacks them (static/offline.html loads none) offers no Remove rather than\n * a control bound to nothing.\n */\n(function () {\n  'use strict';\n\n  /**\n   * Whether this page has what a removal needs.\n   *\n   * @returns {boolean}\n   */\n  function available() {\n    var core = self.pwaBasemapDownloadCore;\n    return !!(\n      core &&\n      typeof core.pinnedCacheName === 'function' &&\n      self.pwaDb &&\n      self.pwaBasemapAreas &&\n      self.caches\n    );\n  }\n\n  /**\n   * Delete whole areas \u2014 each one's pinned bucket AND its record.\n   *\n   * @param {string[]} areaIds Bucket ids, as minted (`region-CH-4115`,\n   *   `custom-\u2026`).\n   * @param {{onBucketDeleted?: function(string): void}} [options]\n   *   `onBucketDeleted` runs once per id after its bucket delete was\n   *   attempted \u2014 the map forgets its byte measurement of the bucket there.\n   * @returns {Promise<boolean>} False when there was nothing to do or no\n   *   core to do it with; true once every step has been attempted.\n   */\n  async function evict(areaIds, options) {\n    var core = self.pwaBasemapDownloadCore;\n    var ids = Array.isArray(areaIds) ? areaIds : [];\n    var hooks = options || {};\n    if (!core || !ids.length) return false;\n\n    await Promise.all(\n      ids.map(async function (areaId) {\n        try {\n          await self.caches.delete(core.pinnedCacheName(areaId));\n        } catch (_e) {\n          // Best-effort.\n        }\n        if (typeof hooks.onBucketDeleted === 'function') hooks.onBucketDeleted(areaId);\n      }),\n    );\n\n    // SNOW-635: `core.isCustomAreaId` \u2014 a custom area's own bucket-id\n    // FAMILY, not the single legacy `CUSTOM_AREA_ID`.\n    var customIds = new Set(\n      ids.filter(function (id) {\n        return core.isCustomAreaId(id);\n      }),\n    );\n    var regionIds = new Set(\n      ids.filter(function (id) {\n        return !core.isCustomAreaId(id);\n      }),\n    );\n    var db = self.pwaDb;\n\n    try {\n      if (customIds.size && db && self.pwaBasemapAreas) {\n        var existing = (await self.pwaBasemapAreas.readCustomAreas()) || [];\n        var next = existing.filter(function (entry) {\n          return !entry || !customIds.has(entry.id);\n        });\n        if (next.length !== existing.length) {\n          // Always a `put`, even for `[]` \u2014 an absent key is what the\n          // custom-area reader treats as \"try the legacy migration\".\n          await db.put('meta:app', {\n            key: self.pwaBasemapAreas.CUSTOM_AREAS_KEY,\n            value: next,\n          });\n        }\n      }\n    } catch (_e) {\n      // Best-effort \u2014 a stale record with no bucket behind it is\n      // evictable-first the next time budget planning runs.\n    }\n\n    try {\n      if (regionIds.size && db) {\n        var row = await db.get('meta:app', 'basemap.regions');\n        var regions = Array.isArray(row && row.value) ? row.value : [];\n        var kept = regions.filter(function (entry) {\n          return !(entry && regionIds.has(core.areaIdForRegion(entry.region_id)));\n        });\n        if (kept.length !== regions.length) {\n          await db.put('meta:app', { key: 'basemap.regions', value: kept });\n        }\n      }\n    } catch (_e) {\n      // Best-effort \u2014 see above.\n    }\n\n    // SNOW-613: the worker has no other way to learn about a page-side\n    // deletion, and a stale name there would be handed to `caches.open`,\n    // recreating the bucket just deleted as an empty one.\n    try {\n      var controller = self.navigator && self.navigator.serviceWorker\n        ? self.navigator.serviceWorker.controller\n        : null;\n      if (controller) controller.postMessage({ type: 'pinned-buckets-changed' });\n    } catch (_e) {\n      // Best-effort.\n    }\n    return true;\n  }\n\n  self.pwaBasemapAreaRemoval = Object.freeze({\n    available: available,\n    evict: evict,\n  });\n})();\n"],
  "mappings": "CAwCC,UAAY,CACX,aAOA,SAASA,GAAY,CACnB,IAAIC,EAAO,KAAK,uBAChB,MAAO,CAAC,EACNA,GACA,OAAOA,EAAK,iBAAoB,YAChC,KAAK,OACL,KAAK,iBACL,KAAK,OAET,CAaA,eAAeC,EAAMC,EAASC,EAAS,CACrC,IAAIH,EAAO,KAAK,uBACZI,EAAM,MAAM,QAAQF,CAAO,EAAIA,EAAU,CAAC,EAC1CG,EAAQF,GAAW,CAAC,EACxB,GAAI,CAACH,GAAQ,CAACI,EAAI,OAAQ,MAAO,GAEjC,MAAM,QAAQ,IACZA,EAAI,IAAI,eAAgBE,EAAQ,CAC9B,GAAI,CACF,MAAM,KAAK,OAAO,OAAON,EAAK,gBAAgBM,CAAM,CAAC,CACvD,MAAa,CAEb,CACI,OAAOD,EAAM,iBAAoB,YAAYA,EAAM,gBAAgBC,CAAM,CAC/E,CAAC,CACH,EAIA,IAAIC,EAAY,IAAI,IAClBH,EAAI,OAAO,SAAUI,EAAI,CACvB,OAAOR,EAAK,eAAeQ,CAAE,CAC/B,CAAC,CACH,EACIC,EAAY,IAAI,IAClBL,EAAI,OAAO,SAAUI,EAAI,CACvB,MAAO,CAACR,EAAK,eAAeQ,CAAE,CAChC,CAAC,CACH,EACIE,EAAK,KAAK,MAEd,GAAI,CACF,GAAIH,EAAU,MAAQG,GAAM,KAAK,gBAAiB,CAChD,IAAIC,EAAY,MAAM,KAAK,gBAAgB,gBAAgB,GAAM,CAAC,EAC9DC,EAAOD,EAAS,OAAO,SAAUE,EAAO,CAC1C,MAAO,CAACA,GAAS,CAACN,EAAU,IAAIM,EAAM,EAAE,CAC1C,CAAC,EACGD,EAAK,SAAWD,EAAS,QAG3B,MAAMD,EAAG,IAAI,WAAY,CACvB,IAAK,KAAK,gBAAgB,iBAC1B,MAAOE,CACT,CAAC,CAEL,CACF,MAAa,CAGb,CAEA,GAAI,CACF,GAAIH,EAAU,MAAQC,EAAI,CACxB,IAAII,EAAM,MAAMJ,EAAG,IAAI,WAAY,iBAAiB,EAChDK,EAAU,MAAM,QAAQD,GAAOA,EAAI,KAAK,EAAIA,EAAI,MAAQ,CAAC,EACzDE,EAAOD,EAAQ,OAAO,SAAUF,EAAO,CACzC,MAAO,EAAEA,GAASJ,EAAU,IAAIT,EAAK,gBAAgBa,EAAM,SAAS,CAAC,EACvE,CAAC,EACGG,EAAK,SAAWD,EAAQ,QAC1B,MAAML,EAAG,IAAI,WAAY,CAAE,IAAK,kBAAmB,MAAOM,CAAK,CAAC,CAEpE,CACF,MAAa,CAEb,CAKA,GAAI,CACF,IAAIC,EAAa,KAAK,WAAa,KAAK,UAAU,cAC9C,KAAK,UAAU,cAAc,WAC7B,KACAA,GAAYA,EAAW,YAAY,CAAE,KAAM,wBAAyB,CAAC,CAC3E,MAAa,CAEb,CACA,MAAO,EACT,CAEA,KAAK,sBAAwB,OAAO,OAAO,CACzC,UAAWlB,EACX,MAAOE,CACT,CAAC,CACH,GAAG",
  "names": ["available", "core", "evict", "areaIds", "options", "ids", "hooks", "areaId", "customIds", "id", "regionIds", "db", "existing", "next", "entry", "row", "regions", "kept", "controller"]
}
