var e=Object.defineProperty,t={};((t,r)=>{for(var n in r)e(t,n,{get:r[n],enumerable:!0})})(t,{createEndpoint:()=>n,expose:()=>expose,proxy:()=>proxy,proxyMarker:()=>r,releaseProxy:()=>a,transfer:()=>transfer,transferHandlers:()=>o,windowEndpoint:()=>windowEndpoint,wrap:()=>wrap});var r=Symbol("Comlink.proxy"),n=Symbol("Comlink.endpoint"),a=Symbol("Comlink.releaseProxy"),s=Symbol("Comlink.thrown"),isObject=e=>"object"==typeof e&&null!==e||"function"==typeof e,o=new Map([["proxy",{canHandle:e=>isObject(e)&&e[r],serialize(e){const{port1:t,port2:r}=new MessageChannel;return expose(e,t),[r,[r]]},deserialize:e=>(e.start(),wrap(e))}],["throw",{canHandle:e=>isObject(e)&&s in e,serialize({value:e}){let t;return t=e instanceof Error?{isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:{isError:!1,value:e},[t,[]]},deserialize(e){if(e.isError)throw Object.assign(new Error(e.value.message),e.value);throw e.value}}]]);function expose(e,t=self){t.addEventListener("message",(function callback(r){if(!r||!r.data)return;const{id:n,type:a,path:o}=Object.assign({path:[]},r.data),i=(r.data.argumentList||[]).map(fromWireValue);let u;try{const t=o.slice(0,-1).reduce(((e,t)=>e[t]),e),n=o.reduce(((e,t)=>e[t]),e);switch(a){case 0:u=n;break;case 1:t[o.slice(-1)[0]]=fromWireValue(r.data.value),u=!0;break;case 2:u=n.apply(t,i);break;case 3:u=proxy(new n(...i));break;case 4:{const{port1:t,port2:r}=new MessageChannel;expose(e,r),u=transfer(t,[t])}break;case 5:u=void 0}}catch(e){u={value:e,[s]:0}}Promise.resolve(u).catch((e=>({value:e,[s]:0}))).then((e=>{const[r,s]=toWireValue(e);t.postMessage(Object.assign(Object.assign({},r),{id:n}),s),5===a&&(t.removeEventListener("message",callback),closeEndPoint(t))}))})),t.start&&t.start()}function closeEndPoint(e){(function isMessagePort(e){return"MessagePort"===e.constructor.name})(e)&&e.close()}function wrap(e,t){return createProxy(e,[],t)}function throwIfProxyReleased(e){if(e)throw new Error("Proxy has been released and is not useable")}function createProxy(e,t=[],r=function(){}){let s=!1;const o=new Proxy(r,{get(r,n){if(throwIfProxyReleased(s),n===a)return()=>requestResponseMessage(e,{type:5,path:t.map((e=>e.toString()))}).then((()=>{closeEndPoint(e),s=!0}));if("then"===n){if(0===t.length)return{then:()=>o};const r=requestResponseMessage(e,{type:0,path:t.map((e=>e.toString()))}).then(fromWireValue);return r.then.bind(r)}return createProxy(e,[...t,n])},set(r,n,a){throwIfProxyReleased(s);const[o,i]=toWireValue(a);return requestResponseMessage(e,{type:1,path:[...t,n].map((e=>e.toString())),value:o},i).then(fromWireValue)},apply(r,a,o){throwIfProxyReleased(s);const i=t[t.length-1];if(i===n)return requestResponseMessage(e,{type:4}).then(fromWireValue);if("bind"===i)return createProxy(e,t.slice(0,-1));const[u,c]=processArguments(o);return requestResponseMessage(e,{type:2,path:t.map((e=>e.toString())),argumentList:u},c).then(fromWireValue)},construct(r,n){throwIfProxyReleased(s);const[a,o]=processArguments(n);return requestResponseMessage(e,{type:3,path:t.map((e=>e.toString())),argumentList:a},o).then(fromWireValue)}});return o}function processArguments(e){const t=e.map(toWireValue);return[t.map((e=>e[0])),(r=t.map((e=>e[1])),Array.prototype.concat.apply([],r))];var r}var i=new WeakMap;function transfer(e,t){return i.set(e,t),e}function proxy(e){return Object.assign(e,{[r]:!0})}function windowEndpoint(e,t=self,r="*"){return{postMessage:(t,n)=>e.postMessage(t,r,n),addEventListener:t.addEventListener.bind(t),removeEventListener:t.removeEventListener.bind(t)}}function toWireValue(e){for(const[t,r]of o)if(r.canHandle(e)){const[n,a]=r.serialize(e);return[{type:3,name:t,value:n},a]}return[{type:0,value:e},i.get(e)||[]]}function fromWireValue(e){switch(e.type){case 3:return o.get(e.name).deserialize(e.value);case 0:return e.value}}function requestResponseMessage(e,t,r){return new Promise((n=>{const a=function generateUUID(){return new Array(4).fill(0).map((()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16))).join("-")}();e.addEventListener("message",(function l(t){t.data&&t.data.id&&t.data.id===a&&(e.removeEventListener("message",l),n(t.data))})),e.start&&e.start(),e.postMessage(Object.assign({id:a},t),r)}))}export{t as Comlink};