const VERSION=1,rootPath=self.location.pathname.replace(/\/service-worker.js$/,""),resourceRoots=[rootPath+"/theia-resource",rootPath+"/vscode-resource"],resolveTimeout=3e4;class RequestStore{constructor(){this.map=new Map}get(e,t){const o=this.map.get(this._key(e,t));return o&&o.promise}create(e,t){const o=this.get(e,t);if(o)return o;let r;const n=new Promise((e=>r=e)),s={resolve:r,promise:n},i=this._key(e,t);this.map.set(i,s);const a=setTimeout((()=>{if(clearTimeout(a),this.map.get(i)===s)return this.map.delete(i)}),3e4);return n}resolve(e,t,o){const r=this.map.get(this._key(e,t));return!!r&&(r.resolve(o),!0)}_key(e,t){return`${e}@@@${t}`}}const resourceRequestStore=new RequestStore,localhostRequestStore=new RequestStore,notFound=()=>new Response("Not Found",{status:404});async function processResourceRequest(e,t,o){const r=await self.clients.get(e.clientId);if(!r)return console.error("Could not find inner client for request"),notFound();const n=getWebviewIdForClient(r),s=t.pathname.startsWith(o+"/")?t.pathname.slice(o.length):t.pathname;function i(e){return e?new Response(e.body,{status:200,headers:{"Content-Type":e.mime}}):notFound()}const a=await getOuterIframeClient(n);if(!a)return console.error("Could not find parent client for request"),notFound();const c=resourceRequestStore.get(n,s);return c?c.then(i):(a.postMessage({channel:"load-resource",path:s,query:t.search.replace(/^\?/,"")}),resourceRequestStore.create(n,s).then(i))}async function processLocalhostRequest(e,t){const o=await self.clients.get(e.clientId);if(!o)return;const r=getWebviewIdForClient(o),n=t.origin,s=o=>{if(!o||t.origin===o)return fetch(e.request);const r=e.request.url.replace(new RegExp(`^${t.origin}(/|$)`),`${o}$1`);return new Response(null,{status:302,headers:{Location:r}})},i=await getOuterIframeClient(r);if(!i)return console.error("Could not find parent client for request"),notFound();const a=localhostRequestStore.get(r,n);return a?a.then(s):(i.postMessage({channel:"load-localhost",origin:n}),localhostRequestStore.create(r,n).then(s))}function getWebviewIdForClient(e){return new URL(e.url).search.match(/\bid=([a-z0-9-]+)/i)[1]}async function getOuterIframeClient(e){return(await self.clients.matchAll({includeUncontrolled:!0})).find((t=>{const o=new URL(t.url);return(o.pathname===`${rootPath}/`||o.pathname===`${rootPath}/index.html`)&&o.search.match(new RegExp("\\bid="+e))}))}self.addEventListener("message",(async e=>{switch(e.data.channel){case"version":return void self.clients.get(e.source.id).then((e=>{e&&e.postMessage({channel:"version",version:1})}));case"did-load-resource":{const t=getWebviewIdForClient(e.source),o=e.data.data,r=200===o.status?{body:o.data,mime:o.mime}:void 0;return void(resourceRequestStore.resolve(t,o.path,r)||console.error("Could not resolve unknown resource",o.path))}case"did-load-localhost":{const t=getWebviewIdForClient(e.source),o=e.data.data;return void(localhostRequestStore.resolve(t,o.origin,o.location)||console.error("Could not resolve unknown localhost",o.origin))}}console.error("Unknown message")})),self.addEventListener("fetch",(e=>{const t=new URL(e.request.url);for(const o of resourceRoots)if(t.origin===self.origin&&t.pathname.startsWith(o+"/"))return e.respondWith(processResourceRequest(e,t,o));if(t.origin!==self.origin&&t.host.match(/^localhost:(\d+)$/))return e.respondWith(processLocalhostRequest(e,t))})),self.addEventListener("install",(e=>{e.waitUntil(self.skipWaiting())})),self.addEventListener("activate",(e=>{e.waitUntil(self.clients.claim())}));