const r=new Map,c=new Map,a=new Map;async function l(){const e=await self.clients.matchAll(),t=new Set(e.map(s=>s.id));Array.from(r.keys()).forEach(s=>{t.has(s)||(r.delete(s),c.delete(s),a.delete(s))})}function h(e,t,s){typeof t=="string"&&typeof s=="string"?r.set(e,{accessToken:t,baseUrl:s}):r.delete(e);const n=c.get(e);n&&(n(r.get(e)),c.delete(e),a.delete(e))}function m(e){const t=a.get(e.id)??new Promise(s=>{c.set(e.id,s),e.postMessage({type:"requestSession"})});return a.has(e.id)||a.set(e.id,t),t}async function p(e,t=3e3){const s=await self.clients.get(e);if(!s)return;const n=m(s),i=new Promise(o=>{setTimeout(()=>o(void 0),t)});return Promise.race([n,i])}self.addEventListener("install",()=>{self.skipWaiting()});self.addEventListener("activate",e=>{e.waitUntil((async()=>{await self.clients.claim(),await l()})())});self.addEventListener("message",e=>{const t=e.source;if(!t)return;const{type:s,accessToken:n,baseUrl:i}=e.data||{};s==="setSession"&&(h(t.id,n,i),l())});const d=["/_matrix/client/v1/media/download","/_matrix/client/v1/media/thumbnail"];function w(e){try{const{pathname:t}=new URL(e);return d.some(s=>t.startsWith(s))}catch{return!1}}function f(e,t){return d.some(s=>{const n=new URL(s,t);return e.startsWith(n.href)})}function u(e){return{headers:{Authorization:`Bearer ${e}`},cache:"default"}}self.addEventListener("fetch",e=>{const{url:t,method:s}=e.request;if(s!=="GET"||!w(t))return;const{clientId:n}=e;if(!n)return;const i=r.get(n);if(i){f(t,i.baseUrl)&&e.respondWith(fetch(t,u(i.accessToken)));return}e.respondWith(p(n).then(o=>o&&f(t,o.baseUrl)?fetch(t,u(o.accessToken)):fetch(e.request)))});
//# sourceMappingURL=sw.js.map
