"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=[{message:"Invalid request. Please try again.",code:400},{message:"Unauthorized access. Please check your credentials.",code:401},{message:"You don't have access to view this resource.",code:403},{message:"The resource you're looking for could not be found.",code:404},{message:"Request timed out. Please try again later.",code:408},{message:"There was a problem with your input. Please check and try again.",code:422},{message:"You have made too many requests. Please wait and try again.",code:429},{message:"Something went wrong on our side. Please try again later.",code:500},{message:"Our servers are currently unreachable. Please try again later.",code:502},{message:"The service is currently unavailable. Please try again later.",code:503},{message:"The server took too long to respond. Please try again later.",code:504}];function y(){typeof window<"u"&&(window.location.href="/login")}const u=2,f=1e4,g=0;class l{constructor(r,o={}){this.url=r,this.options={retries:u,enableGlobalLoader:!1,method:"GET",timeout:f,retryDelay:g,...o},this.controller=new AbortController}createAPIError(r,o,i,a){return{type:r,message:o,code:i,retry:a}}async makeRequest(){const{method:r,body:o,headers:i,timeout:a}=this.options,n={"Content-Type":"application/json",...i},e=setTimeout(()=>this.controller.abort(),a);try{const t=await fetch(this.url,{method:r,headers:n,body:o?JSON.stringify(o):void 0,signal:this.controller.signal});if(!t.ok){const s=await t.json();throw this.createAPIError("custom",(s==null?void 0:s.message)||"An error occurred",t.status,0)}return await t.json()}catch(t){throw t instanceof DOMException&&t.name==="AbortError"?this.createAPIError("timeout","Request timed out",408,0):t instanceof TypeError?this.createAPIError("network",t.message,0,0):this.createAPIError("unknown","An unknown error occurred",0,0)}finally{clearTimeout(e)}}async execute(){const{retries:r=u,enableGlobalLoader:o,onStartLoader:i,onStopLoader:a}=this.options;o&&i&&i();for(let n=0;n<=r;n++)try{const e=await this.makeRequest();return o&&a&&a(),[void 0,e]}catch(e){o&&a&&a();let t;if(e instanceof DOMException&&e.name==="AbortError"?t=this.createAPIError("aborted","Request aborted",0,n):e instanceof Error?t=this.createAPIError("unknown",e.message,0,n):t=this.createAPIError((e==null?void 0:e.type)||"unknown",(e==null?void 0:e.message)||"An unknown error occurred",(e==null?void 0:e.code)||0,n),t.type==="custom"){const s=m.find(h=>h.code===t.code);if(s)return s.code===401&&y(),[{...s,type:"custom"},void 0]}if(n===r)return[t,void 0];this.options.retryDelay&&await new Promise(s=>setTimeout(s,this.options.retryDelay))}return[void 0,void 0]}subscribe(){return[this.execute(),this.unsubscribe.bind(this)]}unsubscribe(){this.controller.abort()}}function d(c,r={}){return new l(c,r).execute()}d.subscribe=function(c,r={}){return new l(c,r).subscribe()};exports.callAPI=d;