import{t as e}from"./arrow-up-down-C4NXV7iY.js";import{$ as t,At as n,Ea as r,En as i,Eu as a,G as o,H as s,J as c,K as l,Nl as u,Pd as d,U as f,V as p,W as m,X as h,ed as g,et as _,ot as v,q as y,ql as b,tr as x,yi as S,zd as C}from"./index-Q5R6-AeA.js";import{t as w}from"./page-layout-Cl29SycZ.js";var T=C(d(),1),E=a(),D=[{id:`key`,accessorKey:`key`,cell:({row:e})=>(0,E.jsx)(f,{children:e.original.key}),header:({column:t})=>(0,E.jsxs)(b,{variant:`ghost`,onClick:()=>t.toggleSorting(t.getIsSorted()===`asc`),children:[`Key`,(0,E.jsx)(e,{})]})},{id:`name`,accessorKey:`name`,cell:({row:e})=>(0,E.jsx)(f,{children:e.original.name}),header:({column:t})=>(0,E.jsxs)(b,{variant:`ghost`,onClick:()=>t.toggleSorting(t.getIsSorted()===`asc`),children:[`Name`,(0,E.jsx)(e,{})]})},{id:`defaultIrsCode`,accessorKey:`defaultIrsCode`,cell:({row:e})=>(0,E.jsx)(f,{className:`flex flex-wrap gap-1 p-1`,children:e.original.defaultIrsCode&&(0,E.jsx)(b,{variant:`outline`,disabled:!0,className:`p-1 w-15`,children:e.original.defaultIrsCode})}),header:`Default IRS Code`},{id:`edit`,cell:({row:e})=>(0,E.jsx)(n,{sortCodeKey:e.original.key,ownerId:e.original.ownerId,onAdd:e.original.refetchSortCodes})}],O=()=>{let[{data:e,fetching:n,error:a},d]=u({query:r}),{setFiltersContext:b}=(0,T.useContext)(x),C=(0,T.useMemo)(()=>e?.allSortCodes?.map(e=>({...e,refetchSortCodes:d}))??[],[e?.allSortCodes,d]),O=c({data:C,columns:D,getCoreRowModel:h(),getPaginationRowModel:t(),getSortedRowModel:_(),initialState:{pagination:{pageSize:100}}});return(0,T.useEffect)(()=>{b((0,E.jsx)(`div`,{className:`flex items-center justify-end gap-10 space-x-2 py-4`,children:(0,E.jsx)(i,{table:O})}))},[b,O,O.getPageOptions()]),(0,T.useEffect)(()=>{a&&(console.error(`Error fetching sort codes`,a),S.error(`Error`,{description:`Error fetching sort codes`}))},[a]),(0,E.jsx)(w,{title:`Sort Codes (${C.length})`,description:`All sort codes`,headerActions:(0,E.jsx)(v,{onAdd:()=>d()}),children:n?(0,E.jsx)(`div`,{className:`flex flex-row justify-center`,children:(0,E.jsx)(g,{className:`h-10 w-10 animate-spin mr-2`})}):(0,E.jsx)(`div`,{className:`flex flex-col gap-4 rounded-md border`,children:(0,E.jsxs)(p,{children:[(0,E.jsx)(o,{children:O.getHeaderGroups().map(e=>(0,E.jsx)(l,{children:e.headers.map(e=>(0,E.jsx)(m,{children:e.isPlaceholder?null:y(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,E.jsx)(s,{children:O.getRowModel().rows.length===0?(0,E.jsx)(l,{children:(0,E.jsx)(f,{colSpan:D.length,className:`text-center py-8`,children:`No sort codes found`})}):O.getRowModel().rows.map(e=>(0,E.jsx)(l,{children:e.getVisibleCells().map(e=>(0,E.jsx)(f,{children:y(e.column.columnDef.cell,e.getContext())},e.id))},e.id))})]})})})};export{O as SortCodes};