import{a0 as F,r as i,j as r,m as C,D as S,a1 as $}from"./entry.client-CV0PzNS2.js";import{u as L}from"./useQuery-_I6Fb0gn.js";import{N as q}from"./NoElementsFound-BYYh6uWM.js";import{P as w}from"./Paginator-CdCtQQNK.js";const A=b=>{var l;const{response:t,infiniteScroll:d,isLoading:g,perPage:x=15,loadItems:f,routableType:c,className:_}=b,{render:m}=F(),s=i.useRef(null);if(i.useEffect(()=>{const a=new IntersectionObserver(n=>{n[0].isIntersecting&&(t==null?void 0:t.page)<(t==null?void 0:t.total_pages)&&!g&&f()},{rootMargin:"100px",threshold:[]});return s.current&&a.observe(s.current),()=>{s.current&&a.unobserve(s.current)}},[g,f,s,t]),g&&!d)return[...Array(x)].map((a,n)=>r.jsx(i.Fragment,{children:m(`${c}-loading`,{})},n));if(!g&&!((l=t==null?void 0:t.items)!=null&&l.length))return r.jsx(q,{});if(t!=null&&t.items)return r.jsxs("div",{className:`grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-4 mt-5 gap-y-8 ${_}`,children:[C(t.items,(a,n)=>r.jsx(i.Fragment,{children:m(`${c}`,a)},a.id??a.code)),!!d&&(t==null?void 0:t.items.length)<t.total_items&&[...Array(x)].map((a,n)=>r.jsx("div",{ref:n===0?s:null,className:"w-full p-2 lg:h-[505px]",children:m(`${c}-loading`,{})},n))]})},D=b=>{const{customRoutablesType:t,showTopPagination:d=!0,className:g}=b,{route:x}=S(),{per_page:f,routables:c,routables_type:_,paginated:m=!0,pagination_url:s,infinite_scroll:l=!1,page:a,total_items:n,total_pages:N,fetch_url:P}=x,j=i.useRef(null),[e,y]=i.useState({page:a,per_page:f,total_items:n,total_pages:N,items:c||[]});i.useEffect(()=>{y(u=>({...u,items:c||[]}))},[c]);const[p,v]=i.useState(1),T=i.useCallback(async()=>{const u={page:p},o=(await $.get(P,{params:u})).data;return y(h=>({...o,items:h!=null&&h.items&&l?h.items.concat(o==null?void 0:o.items):o==null?void 0:o.items})),o},[p,P,l]),{isLoading:E}=L({queryKey:["index-routable",P,p],queryFn:T,refetchOnWindowFocus:!1,enabled:p>1}),I=u=>{v(u),j.current&&j.current.scrollIntoView({behavior:"smooth"})};return r.jsxs("div",{className:"h-full w-full",children:[r.jsx("span",{ref:j}),d&&!l&&e&&m&&e.total_pages>1&&r.jsx(w,{paginationUrl:s,onPageChange:I,currentPage:e==null?void 0:e.page,totalPages:e==null?void 0:e.total_pages}),r.jsx(A,{loadItems:()=>v(u=>u+1),response:e,infiniteScroll:l,isLoading:E,perPage:f,routableType:t||_,className:g}),!l&&e&&m&&e.total_pages>1&&r.jsx(w,{paginationUrl:s,onPageChange:I,currentPage:e==null?void 0:e.page,totalPages:e==null?void 0:e.total_pages})]})};export{D as I};