"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_chunks-cjs/node-request.cjs");const t=["request","response","progress","error","abort"],r=["processOptions","validateOptions","interceptRequest","finalizeOptions","onRequest","onResponse","onError","onReturn","onHeaders"];function n(o,s){const i=[],u=r.reduce((e,t)=>(e[t]=e[t]||[],e),{processOptions:[e.p],validateOptions:[e.v]});function a(e){const r=t.reduce((e,t)=>(e[t]=function(){const e=/* @__PURE__ */Object.create(null);let t=0;return{publish:function(t){for(const r in e)e[r](t)},subscribe:function(r){const n=t++;return e[n]=r,function(){delete e[n]}}}}(),e),{}),n=(e=>function(t,r,...n){const o="onError"===t;let s=r;for(let r=0;r<e[t].length&&(s=(0,e[t][r])(s,...n),!o||s);r++);return s})(u),o=n("processOptions",e);n("validateOptions",o);const i={options:o,channels:r,applyMiddleware:n};let a;const c=r.request.subscribe(e=>{a=s(e,(t,o)=>((e,t,o)=>{let s=e,i=t;if(!s)try{i=n("onResponse",t,o)}catch(e){i=null,s=e}s=s&&n("onError",s,o),s?r.error.publish(s):i&&r.response.publish(i)})(t,o,e))});r.abort.subscribe(()=>{c(),a&&a.abort()});const p=n("onReturn",r,i);return p===r&&r.request.publish(i),p}return a.use=function(e){if(!e)throw new Error("Tried to add middleware that resolved to falsey value");if("function"==typeof e)throw new Error("Tried to add middleware that was a function. It probably expects you to pass options to it.");if(e.onReturn&&u.onReturn.length>0)throw new Error("Tried to add new middleware with `onReturn` handler, but another handler has already been registered for this event");return r.forEach(t=>{e[t]&&u[t].push(e[t])}),i.push(e),a},a.clone=()=>n(i,s),o.forEach(a.use),a}exports.adapter=e.a,exports.environment="node",exports.getIt=(t=[],r=e.h)=>n(t,r);//# sourceMappingURL=index.cjs.map
