import{r as t}from"./index-BsAdyuxG.js";const n={url:null,query:null,type:"get",start:!0,headers:null,data:null,onSuccess:null,onError:null},i=(u=n)=>{const[c,a]=t.useState({data:null,isLoading:!!u.url,isSuccess:!1,isError:!1,error:""}),s=(e=u)=>{if(!e.url)return a(r=>({...r,isLoading:!1}));a(r=>({...r,isLoading:!0}));let l=e.url.includes("?")?"":"?";e.query&&(l+=Object.entries(e.query).map(r=>r.join("=")).join("&"));const f=e.url+l;window.api({url:f,method:e.type||"get",headers:(e==null?void 0:e.headers)||null,data:e.data}).then(({data:r})=>{a({data:r,isLoading:!1,isSuccess:!0,isError:!1,error:""}),e.onSuccess&&e.onSuccess(r)}).catch(r=>{a({data:null,isLoading:!1,isSuccess:!1,isError:!0,error:r.message||"Failed to fetch"}),e.onError&&e.onError(r)}).finally(()=>{e.finally&&e.finally()})};return t.useEffect(s,[]),{...c,setData:e=>{a(l=>({...l,data:e}))},refetch:s}};export{i as u};
