import{r as t}from"./vendor-react-wBh_31cN.js";import{j as R}from"./vendor-redux-CJZrIb6x.js";import{m as C}from"./mergeRight-Cv2eiGXV.js";import{r as U}from"./range-BC8I8D3I.js";import{c as _}from"./chain-B-C0kF16.js";import{a as k}from"./index-BKr2rJhV.js";import{i as q}from"./inc-0XPMXBh9.js";const X=({api:u,apiEndpointName:L,apiArgs:s,limit:c=20,invalidatesTags:m,dataProp:n="items",totalProp:r="count",skip:f=!1,pollingInterval:N})=>{const a=R(),[o,M]=t.useState(1),[p,z]=t.useState(0),[b,g]=t.useState(!1),i=u.endpoints[L],{data:e,isUninitialized:E,isLoading:F,isFetching:O,isError:S,refetch:d}=i.useQuery({...s,params:C(s.params??{},{offset:0,limit:c})},{skip:f,refetchOnFocus:!0,refetchOnMountOrArgChange:!0,pollingInterval:N}),[h,x]=t.useState((e==null?void 0:e[n])??[]),I=t.useCallback(()=>{m&&a(u.util.invalidateTags(m)),d()},[u.util,a,m,d]);t.useEffect(function(){M(1)},[s.params]),t.useEffect(function(){if(!f&&(e&&z(Math.ceil(e[r]/c)),o===1&&x((e==null?void 0:e[n])??[]),o>1)){g(!0);const w=U(0,o).map(l=>a(i.initiate({...s,params:C(s.params??{},{offset:l*c,limit:c})})).unwrap());Promise.all(w).then(l=>{const Q=_(k([],n),l);x(Q)}).catch(console.error).finally(()=>{g(!1)})}},[i,s,a,e,c,o,r,n,f]);const y=t.useCallback(()=>{M(q)},[]);return t.useMemo(()=>({data:h.length===0?(e==null?void 0:e[n])??[]:h,hasMore:o<p,totalItems:(e==null?void 0:e[r])??0,isUninitialized:E,isLoading:F,isFetching:O,isFetchingMore:b,isError:S,loadMore:y,refetch:I}),[n,r,h,e,O,b,S,F,E,p,o,y,I])};export{X as u};
