import{t as e}from"./arrow-up-down-C4NXV7iY.js";import{$ as t,Bi as n,En as r,Eu as i,Fi as a,G as o,H as s,J as c,K as l,Ml as u,Nl as d,Oa as f,Pd as p,Pi as m,Ri as h,Tn as g,U as _,Uu as v,V as y,W as b,Wc as x,X as S,at as C,ed as w,et as T,it as E,pn as D,q as O,ql as k,tr as A,vi as j,vl as M,yi as N,zd as P,zi as F}from"./index-Q5R6-AeA.js";import{t as I}from"./page-layout-Cl29SycZ.js";var L=P(p(),1),R=`updateTaxCategory`,z=()=>{let[{fetching:e},t]=u(M);return{fetching:e,updateTaxCategory:(0,L.useCallback)(async e=>{let n=`Error updating tax category`,r=`${R}-${e.taxCategoryId}`;N.loading(`Uploading Tax Category`,{id:r});try{let i=j(await t(e),n,r,`updateTaxCategory`);if(i)return N.success(`Success`,{id:r,description:`${i.updateTaxCategory.name} was updated`}),i.updateTaxCategory}catch(e){console.error(`${n}: ${e}`),N.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},B=i();function V({taxCategoryId:e,onAdd:t}){let[r,i]=(0,L.useState)(!1);return(0,B.jsxs)(m,{open:r,onOpenChange:i,children:[(0,B.jsx)(n,{asChild:!0,children:(0,B.jsx)(k,{variant:`outline`,size:`icon`,children:(0,B.jsx)(v,{})})}),(0,B.jsxs)(a,{className:`w-[400px] max-w-screen-md`,children:[(0,B.jsx)(h,{children:(0,B.jsx)(F,{children:`Edit Tax Category`})}),(0,B.jsx)(H,{taxCategoryId:e,close:()=>i(!1),onAdd:t})]})]})}function H({taxCategoryId:e,close:t,onAdd:n}){let[{data:r,fetching:i}]=d({query:x,variables:{id:e}}),a=r?.taxCategory;return!a&&!i&&N.error(`Error`,{description:`Couldn'nt find tax category`}),i?(0,B.jsx)(w,{className:`h-10 w-10 animate-spin`}):(0,B.jsx)(U,{taxCategory:a,close:t,onAdd:n})}function U({taxCategory:e,close:t,onAdd:n}){let r=g({defaultValues:{sortCode:e.sortCode?.key,name:e.name,irsCode:e.irsCode}}),{handleSubmit:i}=r,[a,o]=(0,L.useState)(!1),{updateTaxCategory:s,fetching:c}=z(),l=r=>{r.sortCode&&=parseInt(r.sortCode.toString()),s({fields:r,taxCategoryId:e.id}).then(e=>{e?.id&&(n?.(e.id),t())})};return(0,B.jsx)(D,{...r,children:(0,B.jsxs)(`form`,{onSubmit:i(l),children:[(0,B.jsx)(`div`,{className:`flex flex-col gap-4 my-4`,children:(0,B.jsx)(C,{isInsert:!1,formManager:r,setFetching:o,ownerId:e.ownerId??void 0})}),(0,B.jsx)(`div`,{className:`flex justify-end mt-4`,children:(0,B.jsx)(k,{type:`submit`,disabled:c||a,children:`Update`})})]})})}var W=({data:e})=>(0,B.jsx)(`td`,{children:e.name}),G=({data:e})=>(0,B.jsx)(`td`,{children:e.sortCode?(0,B.jsxs)(`span`,{children:[e.sortCode?.name,` (`,e.sortCode?.id,`)`]}):`N/A`}),K=[{id:`name`,accessorKey:`name`,cell:({row:e})=>(0,B.jsx)(W,{data:e.original}),header:({column:t})=>(0,B.jsxs)(k,{variant:`ghost`,onClick:()=>t.toggleSorting(t.getIsSorted()===`asc`),children:[`Name`,(0,B.jsx)(e,{})]})},{id:`sortCode`,accessorKey:`sortCode.key`,cell:({row:e})=>(0,B.jsx)(G,{data:e.original}),header:({column:t})=>(0,B.jsxs)(k,{variant:`ghost`,onClick:()=>t.toggleSorting(t.getIsSorted()===`asc`),children:[`Sort Code`,(0,B.jsx)(e,{})]})},{id:`edit`,cell:({row:e})=>(0,B.jsx)(V,{taxCategoryId:e.original.id})}],q=()=>{let[{data:e,fetching:n,error:i},a]=d({query:f}),{setFiltersContext:u}=(0,L.useContext)(A),p=e?.taxCategories??[],m=c({data:p,columns:K,getCoreRowModel:S(),getPaginationRowModel:t(),getSortedRowModel:T(),initialState:{pagination:{pageSize:30}}});return(0,L.useEffect)(()=>{u((0,B.jsx)(`div`,{className:`flex items-center justify-end gap-10 space-x-2 py-4`,children:(0,B.jsx)(`div`,{className:`flex items-center justify-between px-2`,children:(0,B.jsx)(r,{table:m})})}))},[u,m,m.getPageOptions()]),(0,L.useEffect)(()=>{i&&N.error(`Error`,{description:`Error fetching tax categories`})},[i]),(0,B.jsx)(I,{title:`Tax Categories (${p.length})`,description:`All tax categories`,headerActions:(0,B.jsx)(E,{onAdd:()=>a()}),children:n?(0,B.jsx)(`div`,{className:`flex flex-row justify-center`,children:(0,B.jsx)(w,{className:`h-10 w-10 animate-spin mr-2`})}):(0,B.jsx)(`div`,{className:`flex flex-col gap-4 rounded-md border`,children:(0,B.jsxs)(y,{children:[(0,B.jsx)(o,{children:m.getHeaderGroups().map(e=>(0,B.jsx)(l,{children:e.headers.map(e=>(0,B.jsx)(b,{children:e.isPlaceholder?null:O(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,B.jsx)(s,{children:m.getRowModel().rows.length===0?(0,B.jsx)(l,{children:(0,B.jsx)(_,{colSpan:K.length,className:`text-center py-8`,children:`No tax categories found`})}):m.getRowModel().rows.map(e=>(0,B.jsx)(l,{children:e.getVisibleCells().map(e=>(0,B.jsx)(_,{children:O(e.column.columnDef.cell,e.getContext())},e.id))},e.id))})]})})})};export{q as TaxCategories};