import{xfetch}from"./xfetch.js";export function createAxiosCompat(){async function t(t){const e="string"==typeof t?{url:t}:t,a=e.method?.toUpperCase()??"GET",r=e.baseURL?new URL(e.url??"",e.baseURL).toString():e.url??"",o={...e.headers};let n,s=r;if(e.params){const t=new URLSearchParams;for(const[a,r]of Object.entries(e.params))t.append(a,String(r));s=`${r}?${t.toString()}`}e.data&&("string"==typeof e.data?n=e.data:(n=JSON.stringify(e.data),o["Content-Type"]="application/json"));const i={method:a,headers:o,body:n,timeout:e.timeout,forceXBind:e.forceXBind,disableXBind:e.disableXBind},c=await xfetch(s,i);let u;switch(e.responseType??"json"){case"json":default:u=await c.json();break;case"text":u=await c.text();break;case"blob":u=await c.blob();break;case"arraybuffer":u=await c.arrayBuffer()}if(e.validateStatus&&!e.validateStatus(c.status))throw new Error(`Request failed with status ${c.status}`);const d={};return c.headers.forEach((t,e)=>{d[e]=t}),{data:u,status:c.status,statusText:c.statusText,headers:d,config:e,xbind:{used:c.usedXBind,protocol:c.transport.protocol,latency:c.transport.latency,peerDID:c.transport.peerDID}}}return{request:t,get:async function(e,a){return t({...a,url:e,method:"GET"})},post:async function(e,a,r){return t({...r,url:e,method:"POST",data:a})},put:async function(e,a,r){return t({...r,url:e,method:"PUT",data:a})},patch:async function(e,a,r){return t({...r,url:e,method:"PATCH",data:a})},delete:async function(e,a){return t({...a,url:e,method:"DELETE"})}}}export function createGotCompat(){async function t(t,e){const a="string"==typeof t?{...e,url:t}:t,r=a.method?.toUpperCase()??"GET",o=a.url??"",n={...a.headers};let s,i=o;if(a.searchParams){const t=new URLSearchParams;for(const[e,r]of Object.entries(a.searchParams))t.append(e,String(r));i=`${o}?${t.toString()}`}a.json?(s=JSON.stringify(a.json),n["Content-Type"]="application/json"):a.body&&(s=a.body instanceof Buffer?a.body.toString():a.body);const c={method:r,headers:n,body:s,timeout:"number"==typeof a.timeout?a.timeout:a.timeout?.request,forceXBind:a.forceXBind,disableXBind:a.disableXBind},u=await xfetch(i,c);let d;switch(a.responseType??"text"){case"json":d=await u.json();break;case"text":default:d=await u.text();break;case"buffer":{const t=await u.arrayBuffer();d=Buffer.from(t);break}}const f={};return u.headers.forEach((t,e)=>{f[e]=t}),{body:d,statusCode:u.status,headers:f,url:u.url,requestUrl:i,xbind:{used:u.usedXBind,protocol:u.transport.protocol,latency:u.transport.latency,peerDID:u.transport.peerDID}}}return t.get=(e,a)=>t(e,{...a,method:"GET"}),t.post=(e,a)=>t(e,{...a,method:"POST"}),t.put=(e,a)=>t(e,{...a,method:"PUT"}),t.patch=(e,a)=>t(e,{...a,method:"PATCH"}),t.delete=(e,a)=>t(e,{...a,method:"DELETE"}),t}export function wrapFetch(){return xfetch}