///
((r,e)=>{"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self).AsyncCall={})})(this,(function(r){"use strict";class e extends Error{constructor(r,e,t,n){super(e),this.name=r,this.code=t,this.stack=n}}function t(r,e){return e.message+=`Error ${r}: https://github.com/Jack-Works/async-call-rpc/wiki/Errors#`+r,e}const n={__proto__:null,Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},o="DOMException:",s=r=>(r+"").replace(/^.+\n.+\n/,""),a=()=>{try{return DOMException}catch(r){}},i=r=>"string"==typeof r,c=r=>"boolean"==typeof r,l=r=>"function"==typeof r,u=r=>"object"==typeof r&&null!==r,f="Error",d=void 0,p=r=>Promise.reject(r),y=Array.isArray,m="2.0",h=(r,e,t,n)=>{r===d&&(r=null),Number.isNaN(e=Math.floor(e))&&(e=-1);const o={jsonrpc:m,id:r,error:{code:e,message:t,data:n}};return $(o.error,"data"),o},g=(r,e,t)=>{const{id:n}=r,{code:o,message:s,data:a}=t(e,r);return h(n,o,s,a)},E=(r="",e=-1)=>t=>{let n=w("",(()=>t.message)),s=w(f,((r=t.constructor)=>l(r)&&r.name));const u=a();return u&&t instanceof u&&(s=o+t.name),(i(t)||"number"==typeof t||c(t)||"bigint"==typeof t)&&(s=f,n=t+""),{code:e,message:n,data:r?{stack:r,type:s}:{type:s}}},b=r=>{if(!u(r))return!1;if(!k(r,"jsonrpc"))return!1;if(r.jsonrpc!==m)return!1;if(k(r,"params")){const e=r.params;if(!y(e)&&!u(e))return!1}return!0},k=(r,e)=>e in r,w=(r,e)=>{try{const t=e();return t===d?r:t+""}catch(e){return r}},$=(r,e)=>{r[e]===d&&delete r[e]},S={serialization:r=>r,deserialization:r=>r},P="AsyncCall/",v=Symbol.for(P+"ignored"),_=Symbol.for(P+"notify"),j=Symbol.for(P+"batch"),x=(r,e)=>r[e][_],z=()=>Math.random().toString(36).slice(2),M=r=>void 0===r||r;r.AsyncCall=(r,w)=>{let P=!0,x=d,N=d;const R=async()=>{try{x=await r}catch(r){N=r,Z("AsyncCall failed to start",r)}finally{P=!1}},{serializer:A=S,key:C="rpc",strict:O=!0,log:T=!0,parameterStructures:W="by-position",preferLocalImplementation:I=!1,idGenerator:J=z,mapError:q,logger:D,channel:F,thenable:L}=w;r instanceof Promise?R():(x=r,P=!1);const[U,G]=(r=>{if(!c(r)){const{methodNotFound:e,unknownMessage:t}=r;return[e,t]}return[r,r]})(O),[B,H,K,Q,V,X]=(r=>{if("all"===r)return[!0,!0,!0,!0,!0,!0];if(!c(r)){const{beCalled:e,localError:t,remoteError:n,type:o,requestReplay:s,sendLocalStack:a}=r;return[M(e),M(t),M(n),"basic"!==o,s,a]}return r?[!0,!0,!0,!0]:[]})(T),{log:Y,error:Z=Y,debug:rr=Y,groupCollapsed:er=Y,groupEnd:tr=Y,warn:nr=Y}=D||console,or=new Map,sr=async r=>{let e;try{if(e=await cr(r),b(e))return await dr(e);if(y(e)&&e.every(b)&&0!==e.length)return Promise.all(e.map(dr));if(G){let r=e.id;return r===d&&(r=null),(r=>h(r,-32600,"Invalid Request"))(r)}return d}catch(r){return H&&Z(r,e,void 0),((r,e)=>{const t=g({},r,e),n=t.error;return n.code=-32700,n.message="Parse error",t})(r,q||E(r&&r.stack))}},ar=async r=>{if(r){if(y(r)){const e=r.filter((r=>r&&k(r,"id")));if(0===e.length)return;return ir(e)}return ir(r)}},ir=r=>A.serialization(r),cr=r=>A.deserialization(r);var lr;if(k(lr=F,"setup")&&l(lr.setup)&&F.setup((r=>sr(r).then(ar)),(r=>{const e=cr(r);return!!b(e)||(r=>Promise.resolve(r))(e).then(b)})),(r=>k(r,"send")&&l(r.send))(F)){const r=F;r.on&&r.on((e=>sr(e).then(ar).then((e=>e&&r.send(e)))))}function ur(r,e,t){return u(r)&&k(r,"stack")&&(r.stack=e.split("\n").reduce(((r,e)=>r.replace(e+"\n","")),""+r.stack)),H&&Z(r),g(t,r,q||E(X?r.stack:d))}async function fr(r,e=!1){e&&(r=[...r]);const t=await ir(r);return F.send(t)}const dr=async r=>{if(k(r,"method")){const e=(async r=>{if(P)await R();else if(N)return ur(N,"",r);let e="";try{const{params:t,method:n,id:o,remoteStack:a}=r,i=n.startsWith("rpc.")?Symbol.for(n):n,c=x&&x[i];if(!l(c))return U?h(o,-32601,"Method not found"):void(H&&rr("Missing method",i,r));const u=y(t)?t:[t];e=s(Error().stack);const f=new Promise((r=>r(c.apply(x,u))));if(B)if(Q){const r=[`${C}.%c${n}%c(${u.map((()=>"%o")).join(", ")}%c)\n%o %c@${o}`,"color: #d2c057","",...u,"",f,"color: gray; font-style: italic;"];V&&r.push((()=>{debugger;return c.apply(x,u)})),a?(er(...r),Y(a),tr()):Y(...r)}else Y(`${C}.${n}(${""+[...u]}) @${o}`);if(await f===v)return;return((r,e)=>{const t={jsonrpc:m,id:r,result:e};return $(t,"id"),t})(o,await f)}catch(t){return ur(t,e,r)}})(r);if(k(r,"id"))return e;try{await e}catch(r){}return d}return(async r=>{let t="",s="",c=0,l=f;if(k(r,"error")){const e=r.error;t=e.message,c=e.code;const n=e.data;s=u(n)&&k(n,"stack")&&i(n.stack)?n.stack:"",l=u(n)&&k(n,"type")&&i(n.type)?n.type:f,K&&(Q?Z(`${l}: ${t}(${c}) %c@${r.id}\n%c${s}`,"color: gray",""):Z(`${l}: ${t}(${c}) @${r.id}\n${s}`))}if(null===r.id||r.id===d)return;const{f:[p,y]=[null,null],stack:m=""}=or.get(r.id)||{};p&&y&&(or.delete(r.id),k(r,"error")?y(((r,t,s,i)=>{try{const c=a();if(r.startsWith(o)&&c)return new c(t,r.slice(13));if(r in n){const e=new n[r](t);return e.stack=i,e.code=s,e}return new e(r,t,s,i)}catch(e){return Error(`E${s} ${r}: ${t}\n${i}`)}})(l,t,c,s+"\n аt AsyncCall (rpc) \n"+m)):p(r.result))})(r)};return new Proxy({__proto__:null},{get(r,e){if("then"===e&&(L===d&&nr(t(3,new TypeError("RPC used as Promise: "))),!0!==L))return d;if(i(e)&&r[e])return r[e];const n=r=>(...n)=>{let o=s(Error().stack),a=d;if(e===j&&(a=n.shift(),e=n.shift()),"symbol"==typeof e){const r=Symbol.keyFor(e)||e.description;if(r){if(!r.startsWith("rpc."))return p(new TypeError("Not start with rpc."));e=r}}else if(e.startsWith("rpc."))return p(t(2,new TypeError));return new Promise(((t,s)=>{if(I&&!P&&i(e)){const r=x&&x[e];if(l(r))return t(r(...n))}const c=J(),[f]=n,p=X?o:"",y="by-name"===W&&1===n.length&&u(f)?f:n,h=((r,e,t,n)=>{const o={jsonrpc:m,id:r,method:e,params:t,remoteStack:n};return $(o,"id"),((r,e)=>{r[e]||delete r[e]})(o,"remoteStack"),o})(r?d:c,e,y,p);if(a?(a.push(h),a.r||(a.r=[()=>fr(a,!0),r=>((r,e)=>{for(const t of r)if(k(t,"id")){const r=or.get(t.id);r&&r.f[1](e)}})(a,r)])):fr(h).catch(s),r)return t();or.set(c,{f:[t,s],stack:o})}))},o=n(!1);return o[_]=n(!0),o[_][_]=o[_],i(e)&&Object.defineProperty(r,e,{value:o,configurable:!0}),o}})},r.JSONSerialization=(r=[d,d],e,t="null")=>({serialization(n){if(t&&u(n)&&k(n,"result")&&n.result===d){const r={...n};r.result=null,"keep"===t&&(r.undef=!0),n=r}return JSON.stringify(n,r[0],e)},deserialization(e){const t=JSON.parse(e,r[1]);return u(t)&&k(t,"result")&&null===t.result&&k(t,"undef")&&!0===t.undef&&(t.result=d,delete t.undef),t}}),r.NoSerialization=S,r.batch=r=>{let e=[];return[new Proxy({__proto__:null},{get(t,n){if(i(n)&&t[n])return t[n];const o=(...t)=>r[j](e,n,...t);return(o[_]=(...t)=>r[j][_](e,n,...t))[_]=o[_],i(n)&&Object.defineProperty(t,n,{value:o,configurable:!0}),o}}),(r=e.r)=>r&&r[0](),(r=Error("Aborted"),t=e.r)=>{t&&t[1](r),e=[]}]},r.notify=r=>l(r)?r[_]:new Proxy(r,{get:x}),Object.defineProperty(r,"__esModule",{value:!0})}));
//# sourceMappingURL=base.min.js.map