import{r as t,i as w}from"./index-D0o5GN6H.js";const Q=(p,D={},y=[])=>{const{initialItemsPerPage:I=10,initialPage:T=1,initialSort:b={field:"createdAt",direction:"DESC"},initialFilters:F={}}=D,[C,i]=t.useState([]),[j,c]=t.useState(0),[g,f]=t.useState(T),[s,L]=t.useState(I),[M,m]=t.useState(!1),[R,S]=t.useState(null),[n,$]=t.useState(b),[u,v]=t.useState(F),[E,d]=t.useState(0),[l,x]=t.useState(""),[z,h]=t.useState(0),[U,A]=t.useState(null),[q,O]=t.useState(0);return t.useEffect(()=>{(async()=>{m(!0),S(null);try{const r=new URLSearchParams({page:g.toString(),limit:s.toString(),sort:n.field,order:n.direction});l&&r.append("search",l),Object.entries(u).forEach(([P,o])=>{o!=null&&o!==""&&r.append(P,o)}),console.log("Fetching data with params:",{page:g,limit:s,sort:n.field,order:n.direction,search:l,filters:u});const e=await w.get(`${p}?${r}`);if(e.data&&typeof e.data=="object")if(A(e.data),e.data.data&&Array.isArray(e.data.data))i(e.data.data),c(e.data.total||0),d(e.data.totalPages||Math.ceil((e.data.total||0)/s)),h(e.data.totalAmount||0);else if(Array.isArray(e.data))i(e.data),c(e.data.length),d(Math.ceil(e.data.length/s)),h(0);else throw console.error("Unexpected response format:",e.data),new Error("Invalid response format from server");else throw console.error("Invalid response:",e),new Error("Invalid response from server");console.log(C)}catch(r){console.error("Error fetching data:",r),S(r.message||"Error fetching data"),i([]),c(0),d(0),h(0)}finally{m(!1)}})()},[p,g,s,n,u,l,q]),t.useEffect(()=>{y.length>0&&(async()=>{m(!0),S(null);try{const r=new URLSearchParams({page:g.toString(),limit:s.toString(),sort:n.field,order:n.direction});l&&r.append("search",l),Object.entries(u).forEach(([P,o])=>{o!=null&&o!==""&&r.append(P,o)});const e=await w.get(`${p}?${r}`);if(e.data&&typeof e.data=="object")if(A(e.data),e.data.data&&Array.isArray(e.data.data))i(e.data.data),c(e.data.total||0),d(e.data.totalPages||Math.ceil((e.data.total||0)/s)),h(e.data.totalAmount||0);else if(Array.isArray(e.data))i(e.data),c(e.data.length),d(Math.ceil(e.data.length/s)),h(0);else throw new Error("Invalid response format from server");else throw new Error("Invalid response from server")}catch(r){S(r.message||"Error fetching data"),i([]),c(0),d(0),h(0)}finally{m(!1)}})()},y),{data:C,totalItems:j,currentPage:g,totalPages:E,itemsPerPage:s,isLoading:M,error:R,sort:n,filters:u,searchTerm:l,totalAmount:z,responseData:U,handlePageChange:a=>{console.log("useDataTable: handlePageChange called with page:",a),a>=1&&a<=E&&f(a)},handleSortChange:a=>{$(a),f(1)},handleFilterChange:a=>{v(a),f(1)},handlePageSizeChange:a=>{console.log("useDataTable: handlePageSizeChange called with size:",a),L(a),f(1)},handleSearchChange:a=>{x(a),f(1)},refresh:()=>{O(a=>a+1)}}};export{Q as u};
