{
  "version": 3,
  "sources": ["api-cache.js"],
  "sourcesContent": ["import { getSiteContextUuid } from \"./auth.js\";\nimport { config } from \"./config.js\";\nimport { readStored, removeStored, writeStored } from \"./stored.js\";\n\n/**\n * The endpoints whose answers are kept between pages, and for how long. They\n * are about the account rather than a page, and nearly every page of the panel\n * asks for them - several components at once.\n */\nconst KEPT = {\n    \"auth.currentUser\": 5 * 60 * 1000,\n    \"plans.current\": 5 * 60 * 1000,\n    \"billing.subscription\": 5 * 60 * 1000,\n    \"billing.offers\": 5 * 60 * 1000,\n    \"billing.purchaseOptions\": 5 * 60 * 1000,\n    \"billing.profile\": 5 * 60 * 1000,\n    \"galleries.defaults\": 5 * 60 * 1000,\n    // Also changed by the staff publishing a document and by a deadline\n    // passing, so it is noticed within these minutes, not at once. Only the\n    // prompt reads it: the API refuses the restricted actions on its own.\n    \"legal.status\": 5 * 60 * 1000,\n};\n\n/** A kept \"there is none\" - a photographer with no subscription or billing details. */\nconst KEPT_FAILURES = [404];\n\nconst KEY_PREFIX = \"api:\";\n\n/** Requests on their way, by key - shared by everyone asking while they are. */\nconst inFlight = new Map();\n\n/** Every GET is told apart by its URL, the site it is about and its language. */\nexport function cacheKey(url) {\n    return `${KEY_PREFIX}${config.language}|${getSiteContextUuid() ?? \"\"}|${url}`;\n}\n\nfunction isKept(name) {\n    return name in KEPT;\n}\n\nexport function inFlightRequest(key) {\n    return inFlight.get(key) ?? null;\n}\n\n/** `request` is on its way under `key` until `settled` settles. */\nexport function trackInFlight(key, request, settled) {\n    inFlight.set(key, request);\n    settled.finally(() => dropInFlight(key, request));\n}\n\n/** `request` is no longer one to share - settled, or given up on. */\nexport function dropInFlight(key, request) {\n    if (inFlight.get(key) === request) {\n        inFlight.delete(key);\n    }\n}\n\n/** `{ ok, data }` or `{ ok: false, status, responseJSON }` kept under `key`, or null. */\nexport function keptAnswer(key) {\n    const kept = readStored(key);\n    if (!kept || kept.expires < Date.now()) {\n        return null;\n    }\n    return kept.answer;\n}\n\n/** Keeps what the API answered `name`, if it is one of `KEPT` and worth keeping. */\nexport function keepAnswer(name, key, result) {\n    if (!isKept(name) || generation !== result.generation) {\n        return;\n    }\n    let answer = null;\n    if (result.ok) {\n        answer = { ok: true, data: result.results[0] };\n    } else {\n        const [response] = result.results;\n        if (KEPT_FAILURES.includes(response.status)) {\n            answer = {\n                ok: false,\n                status: response.status,\n                responseJSON: response.responseJSON,\n            };\n        }\n    }\n    if (answer) {\n        writeStored(key, { expires: Date.now() + KEPT[name], answer });\n    }\n}\n\n/**\n * Bumped by every forgetting, so that an answer asked for before it is not\n * kept after it.\n */\nlet generation = 0;\n\nexport function currentGeneration() {\n    return generation;\n}\n\n/**\n * Forgets every kept answer, in this tab and the others: something changed on\n * the account, or the API refused something it was asked.\n */\nexport function forgetAnswers() {\n    generation += 1;\n    inFlight.clear();\n    try {\n        Object.keys(localStorage)\n            .filter((key) => key.startsWith(`stored:${KEY_PREFIX}`))\n            .forEach((key) => removeStored(key.slice(\"stored:\".length)));\n    } catch {\n        // Nothing could have been kept either.\n    }\n}\n\n// Another tab forgot them: so does this one's in-flight requests.\nwindow.addEventListener(\"storage\", (event) => {\n    if (event.key === null || event.key.startsWith(`stored:${KEY_PREFIX}`)) {\n        if (event.newValue === null) {\n            generation += 1;\n            inFlight.clear();\n        }\n    }\n});\n"],
  "mappings": "AAAA,OAAS,sBAAAA,MAA0B,YACnC,OAAS,UAAAC,MAAc,cACvB,OAAS,cAAAC,EAAY,gBAAAC,EAAc,eAAAC,MAAmB,cAOtD,MAAMC,EAAO,CACT,mBAAoB,IAAS,IAC7B,gBAAiB,IAAS,IAC1B,uBAAwB,IAAS,IACjC,iBAAkB,IAAS,IAC3B,0BAA2B,IAAS,IACpC,kBAAmB,IAAS,IAC5B,qBAAsB,IAAS,IAI/B,eAAgB,IAAS,GAC7B,EAGMC,EAAgB,CAAC,GAAG,EAEpBC,EAAa,OAGbC,EAAW,IAAI,IAGd,gBAAS,SAASC,EAAK,CAC1B,MAAO,GAA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D,QAASK,GAAQT,EAAaS,EAAI,MAAM,CAAgB,CAAC,CAAC,CACnE,MAAQ,CAER,CACJ,CAGA,OAAO,iBAAiB,UAAYQ,GAAU,EACtCA,EAAM,MAAQ,MAAQA,EAAM,IAAI,WAAW,UAAUb,CAAU,EAAE,IAC7Da,EAAM,WAAa,OACnBH,GAAc,EACdT,EAAS,MAAM,EAG3B,CAAC",
  "names": ["getSiteContextUuid", "config", "readStored", "removeStored", "writeStored", "KEPT", "KEPT_FAILURES", "KEY_PREFIX", "inFlight", "url", "isKept", "name", "key", "request", "settled", "kept", "result", "generation", "answer", "response", "event"]
}
