import t,{useMemo as u,useState as b,useEffect as z}from"react";import{useReactTable as G,getCoreRowModel as $,getSortedRowModel as k,getExpandedRowModel as A}from"@tanstack/react-table";import g from"styled-components";import{useWindowVirtualizer as I}from"@tanstack/react-virtual";import{SpinnerLoader as H}from"@redocly/theme/components/Loaders/SpinnerLoader";import{GridRow as p}from"./GridRow";import{GridHeader as L}from"./GridHeader";const O=({data:i,columns:n,sortBy:l,empty:w,subRowKey:s="subrows",slug:f,groupHeaderTitle:E})=>{const R=u(()=>l||[],[l]),h=G({columns:n,data:i,getCoreRowModel:$(),getSortedRowModel:k(),getExpandedRowModel:A(),getSubRows:e=>{const o=e?.[s];return Array.isArray(o)?o:[]},...l?{initialState:{sorting:R}}:{},autoResetExpanded:!1}),{getHeaderGroups:v,getRowModel:y}=h,x=v(),{rows:C}=y(),r=C.filter(e=>e.depth===0),[d,M]=b(!1);z(()=>{M(!0)},[]);const m=I({count:r.length,estimateSize:()=>54,overscan:15,getItemKey(e){return`vi-row-${r[e].id}-${r.length}`}}),S=d?m.getVirtualItems():[],T=r.slice(0,50),a=u(()=>i.some(o=>{const c=o[s];return Array.isArray(c)&&c.length>0})?`50px repeat(${n.length-1}, 1fr)`:`25% repeat(${n.length-1}, 1fr)`,[n.length,i,s]);return t.createElement("div",{role:"table"},t.createElement(L,{headerGroups:x,gridTemplateColumns:a,groupHeaderTitle:E}),t.createElement(V,{style:{height:d?m.getTotalSize():"auto"},role:"rowgroup"},r.length>0?d?S.map(e=>{const o=r[e.index];return t.createElement(p,{key:e.key,variant:"virtualized",row:o,slug:f,virtualRow:e,virtualizer:m,gridTemplateColumns:a})}):t.createElement(t.Fragment,null,T.map(e=>t.createElement(p,{key:`ssr-row-${e.id}`,variant:"ssr",row:e,gridTemplateColumns:a})),t.createElement(j,null,t.createElement(H,{color:"var(--link-color-primary)",size:"20px"}))):t.createElement(p,{variant:"noData",empty:w,gridTemplateColumns:a})))},V=g.div`
  position: relative;
`,j=g.div`
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px;
`;export{O as Grid};
