const e=self,t=new Map,n=8e3;function r(e){let r=`${e.databaseName}@${e.version}`,i=t.get(r);if(i)return i;let a=new Promise((i,o)=>{let s=!1,c,l=()=>s?!1:(s=!0,clearTimeout(c),!0);c=setTimeout(()=>{l()&&o(Error(`IDB open timed out after ${n}ms: ${r}`))},n);let u=indexedDB.open(e.databaseName,e.version);u.onerror=()=>{l()&&o(u.error)},u.onblocked=()=>{l()&&o(Error(`IDB open blocked by an older open connection: ${r}`))},u.onsuccess=()=>{let e=u.result;if(!l()){e.close();return}let n=()=>{t.get(r)===a&&t.delete(r)};e.onversionchange=()=>{e.close(),n()},e.onclose=n,i(e)},u.onupgradeneeded=t=>{let n=t.target.result;for(let t of e.stores)n.objectStoreNames.contains(t)||n.createObjectStore(t)}});return t.set(r,a),a.catch(()=>{t.get(r)===a&&t.delete(r)}),a}async function i(e,t,n){let i=(await r(e)).transaction(t,`readonly`).objectStore(t);return new Promise((e,t)=>{let r=i.get(n);r.onsuccess=()=>e(r.result??null),r.onerror=()=>t(r.error)})}async function a(e,t,n){let i=(await r(e)).transaction(t,`readonly`).objectStore(t),a=n.map(e=>new Promise((t,n)=>{let r=i.get(e);r.onsuccess=()=>t(r.result??null),r.onerror=()=>n(r.error)}));return Promise.all(a)}async function o(e,t,n,i){let a=(await r(e)).transaction(t,`readwrite`).objectStore(t);return new Promise((e,t)=>{let r=a.put(i,n);r.onsuccess=()=>e(),r.onerror=()=>t(r.error)})}async function s(e,t,n){let i=(await r(e)).transaction(t,`readwrite`).objectStore(t);return new Promise((e,t)=>{let r=i.delete(n);r.onsuccess=()=>e(),r.onerror=()=>t(r.error)})}async function c(e,t){let n=(await r(e)).transaction(t,`readwrite`).objectStore(t);return new Promise((e,t)=>{let r=n.clear();r.onsuccess=()=>e(),r.onerror=()=>t(r.error)})}async function l(e,t){let n=(await r(e)).transaction(t,`readonly`).objectStore(t);return new Promise((e,t)=>{let r=n.getAllKeys();r.onsuccess=()=>e(r.result),r.onerror=()=>t(r.error)})}async function u(e,t){let n=(await r(e)).transaction(t,`readonly`).objectStore(t);return new Promise((e,t)=>{let r=n.count();r.onsuccess=()=>e(r.result),r.onerror=()=>t(r.error)})}async function d(e,t){let n=(await r(e)).transaction(t,`readonly`).objectStore(t);return new Promise((e,t)=>{let r=n.getAll();r.onsuccess=()=>e(r.result),r.onerror=()=>t(r.error)})}e.addEventListener(`message`,async t=>{let n=t.data;try{let t;switch(n.type){case`get`:t=await i(n.db,n.store,n.key);break;case`getBatch`:t=await a(n.db,n.store,n.keys);break;case`set`:await o(n.db,n.store,n.key,n.value),t=void 0;break;case`del`:await s(n.db,n.store,n.key),t=void 0;break;case`clear`:await c(n.db,n.store),t=void 0;break;case`keys`:t=await l(n.db,n.store);break;case`count`:t=await u(n.db,n.store);break;case`getAll`:t=await d(n.db,n.store);break;default:throw Error(`Unknown IDB operation: ${n.type}`)}let r={id:n.id,type:`success`,result:t};e.postMessage(r)}catch(t){let r={id:n.id,type:`error`,error:t instanceof Error?t.message:`Unknown error`};e.postMessage(r)}});