const I=(e,t)=>t.some((t=>e instanceof t));let w,b;function C(){return w||(w=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])}function x(){return b||(b=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])}const l=new WeakMap,h=new WeakMap,f=new WeakMap;function V(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(a(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)}));return f.set(t,e),t}function S(e){if(l.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)}));l.set(e,t)}let y={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return l.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return a(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function L(e){y=e(y)}function T(e){return x().includes(e)?function(...t){return e.apply(m(this),t),a(this.request)}:function(...t){return a(e.apply(m(this),t))}}function j(e){return"function"==typeof e?T(e):(e instanceof IDBTransaction&&S(e),I(e,C())?new Proxy(e,y):e)}function a(e){if(e instanceof IDBRequest)return V(e);if(h.has(e))return h.get(e);const t=j(e);return t!==e&&(h.set(e,t),f.set(t,e)),t}const m=e=>f.get(e);function F(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const i=indexedDB.open(e,t),c=a(i);return r&&i.addEventListener("upgradeneeded",(e=>{r(a(i.result),e.oldVersion,e.newVersion,a(i.transaction),e)})),n&&i.addEventListener("blocked",(e=>n(e.oldVersion,e.newVersion,e))),c.then((e=>{s&&e.addEventListener("close",(()=>s())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),c}function k(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(e=>t(e.oldVersion,e))),a(n).then((()=>{}))}const A=["get","getKey","getAll","getAllKeys","count"],O=["put","add","delete","clear"],D=new Map;function E(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(D.get(t))return D.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=O.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!A.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let i=s.store;return r&&(i=i.index(t.shift())),(await Promise.all([i[n](...t),o&&s.done]))[0]};return D.set(t,s),s}L((e=>({...e,get:(t,n,r)=>E(t,n)||e.get(t,n,r),has:(t,n)=>!!E(t,n)||e.has(t,n)})));const v=["continue","continuePrimaryKey","advance"],M={},B=new WeakMap,g=new WeakMap,W={get(e,t){if(!v.includes(t))return e[t];let n=M[t];return n||(n=M[t]=function(...e){B.set(this,g.get(this)[t](...e))}),n}};async function*p(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const n=new Proxy(t,W);for(g.set(n,t),f.set(n,m(t));t;)yield n,t=await(B.get(n)||t.continue()),B.delete(n)}function P(e,t){return t===Symbol.asyncIterator&&I(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===t&&I(e,[IDBIndex,IDBObjectStore])}L((e=>({...e,get:(t,n,r)=>P(t,n)?p:e.get(t,n,r),has:(t,n)=>P(t,n)||e.has(t,n)})));export{k as d,F as o};