import{r as s}from"./vendor-react-wBh_31cN.js";import{j as x}from"./vendor-redux-CJZrIb6x.js";import{u as A}from"./index-DLI8oDiK.js";import{aI as C}from"./index-dNe6i2rS.js";const D=t=>{var u,c;const i=((u=t==null?void 0:t.response)==null?void 0:u.status)??(t==null?void 0:t.status)??(t==null?void 0:t.originalStatus),e=((c=t==null?void 0:t.response)==null?void 0:c.data)??(t==null?void 0:t.data);return{message:(e==null?void 0:e.usrMsg)||(e==null?void 0:e.errorMsg)||(e==null?void 0:e.devMsg)||(e==null?void 0:e.message)||(t==null?void 0:t.message)||"Request failed",status:i}},P=({pId:t,searchTerm:i,sortParam:e,enabled:l=!0,latestVersionOnly:u=!0})=>{const c=x(),{data:a,isLoading:j}=A(+t,{skip:!l}),[g,p]=s.useState([]),[w,h]=s.useState([]),[N,v]=s.useState(!1),[R,E]=s.useState(!1),[S,k]=s.useState(0);s.useEffect(()=>{if(!l)return;if(!(a!=null&&a.length)){a&&(p([]),h([]),E(!0));return}let o=!1;v(!0);const G=a.map(r=>c(C.getFeatureGroups.initiate({pId:t,fsId:r.featurestoreId,params:{filter_by:[...u?["latest_version"]:[],...i?[`NAME_LIKE:${i}`]:[]],...e?{sort_by:e}:{},offset:0,limit:1e3}},{forceRefetch:S>0})).unwrap());return Promise.allSettled(G).then(r=>{if(o)return;const F=[],I=[],L=new Set;r.forEach((m,M)=>{const d=a[M];if(m.status==="fulfilled")(m.value.items??[]).forEach(n=>{n.id!=null&&L.has(n.id)||(n.id!=null&&L.add(n.id),F.push({...n,__sharedFromProjectId:d.projectId,__sharedFromProjectName:d.projectName}))});else{const{message:_,status:n}=D(m.reason);I.push({projectName:d.projectName,featurestoreId:d.featurestoreId,message:_,status:n})}}),p(F),h(I),v(!1),E(!0)}),()=>{o=!0}},[c,a,t,i,e,S,l,u]);const f=s.useCallback(()=>{k(o=>o+1)},[]);return s.useEffect(()=>{const o=()=>{document.visibilityState==="visible"&&f()};return window.addEventListener("focus",f),document.addEventListener("visibilitychange",o),()=>{window.removeEventListener("focus",f),document.removeEventListener("visibilitychange",o)}},[f]),{data:g,isLoading:j||!R,isFetching:N,refetch:f,totalItems:g.length,errors:w}};export{P as u};
