import{v as e,p as t}from"./defaultOptionsValidator.js";const r=["request","response","progress","error","abort"],o=["processOptions","validateOptions","interceptRequest","finalizeOptions","onRequest","onResponse","onError","onReturn","onHeaders"];function n(s,i){const u=[],a=o.reduce((e,t)=>(e[t]=e[t]||[],e),{processOptions:[t],validateOptions:[e]});function c(e){const t=r.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 o=t++;return e[o]=r,function(){delete e[o]}}}}(),e),{}),o=(e=>function(t,r,...o){const n="onError"===t;let s=r;for(let r=0;r<e[t].length&&(s=(0,e[t][r])(s,...o),!n||s);r++);return s})(a),n=o("processOptions",e);o("validateOptions",n);const s={options:n,channels:t,applyMiddleware:o};let u;const c=t.request.subscribe(e=>{u=i(e,(r,n)=>((e,r,n)=>{let s=e,i=r;if(!s)try{i=o("onResponse",r,n)}catch(e){i=null,s=e}s=s&&o("onError",s,n),s?t.error.publish(s):i&&t.response.publish(i)})(r,n,e))});t.abort.subscribe(()=>{c(),u&&u.abort()});const l=o("onReturn",t,s);return l===t&&t.request.publish(s),l}return c.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&&a.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 o.forEach(t=>{e[t]&&a[t].push(e[t])}),u.push(e),c},c.clone=()=>n(u,i),s.forEach(c.use),c}export{n as c};//# sourceMappingURL=createRequester.js.map
