const m=t=>r=>{const e=t(r);return r.add(e),e},O=t=>(r,e)=>(t.set(r,e),e),h=Number.MAX_SAFE_INTEGER===void 0?9007199254740991:Number.MAX_SAFE_INTEGER,p=536870912,l=p*2,b=(t,r)=>e=>{const a=r.get(e);let n=a===void 0?e.size:a<l?a+1:0;if(!e.has(n))return t(e,n);if(e.size<p){for(;e.has(n);)n=Math.floor(Math.random()*l);return t(e,n)}if(e.size>h)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;e.has(n);)n=Math.floor(Math.random()*h);return t(e,n)},w=new WeakMap,A=O(w),g=b(A,w),y=m(g),I=(t,r,e,a)=>n=>{const d=r(n);return u=>{const i=t(u);u.addEventListener("message",(({data:s})=>{const{id:o}=s;if(o!==null&&i.has(o)){const{reject:c,resolve:E}=i.get(o);i.delete(o),s.error===void 0?E(s.result):c(new Error(s.error.message))}})),a(u)&&u.start();const M=(s,o=null,c=[])=>new Promise((E,N)=>{const _=e(i);i.set(_,{reject:N,resolve:E}),o===null?u.postMessage({id:_,method:s},c):u.postMessage({id:_,method:s,params:o},c)}),T=(s,o,c=[])=>{u.postMessage({id:null,method:s,params:o},c)};let f={};for(const[s,o]of Object.entries(d))f={...f,[s]:o({call:M,notify:T})};return{...f}}},W=t=>r=>{if(t.has(r))return t.get(r);const e=new Map;return t.set(r,e),e},k=t=>r=>({...r,connect:({call:e})=>async()=>{const{port1:a,port2:n}=new MessageChannel,d=await e("connect",{port:a},[a]);return t.set(n,d),n},disconnect:({call:e})=>async a=>{const n=t.get(a);if(n===void 0)throw new Error("The given port is not connected.");await e("disconnect",{portId:n})},isSupported:({call:e})=>()=>e("isSupported")}),R=t=>typeof t.start=="function",q=I(W(new WeakMap),k(new WeakMap),g,R);export{y as a,q as c,g};
