import{L as N,r as t,a as G,b as o,I as M,j as m,B as R,q as w,A as U,ac as B,e as K,Y as Q,g as z,T as A,Q as V,be as Y}from"./index-ktJf6c6X.js";const H=N.injectEndpoints({endpoints:n=>({createStoneConversion:n.mutation({query:({stoneConversion:s})=>({url:"stone-conversion-configuration",method:"POST",body:s})}),getStoneConversionList:n.query({query:()=>({url:"stone-conversion-configuration",method:"GET"})}),checkDuplicateStone:n.mutation({query:({stoneConversion:s})=>({url:"stone-conversion-configuration/duplicate",method:"POST",body:s})}),updateStoneConversion:n.mutation({query:({stoneConversion:s})=>({url:"stone-conversion-configuration",method:"PUT",body:s})})})}),{useCreateStoneConversionMutation:J,useLazyGetStoneConversionListQuery:X,useCheckDuplicateStoneMutation:ne,useUpdateStoneConversionMutation:Z}=H,v={id:0,tons:1,sqft:0};function te(){const[n,s]=t.useState(v),[r,D]=t.useState(!1),[L,g]=t.useState(!1),[i,a]=t.useState({}),[S,P]=t.useState([]),{openSnackbar:b}=G(),[j,u]=t.useState(null),[l,{data:d,isLoading:F,error:y}]=X(),[q,{isLoading:_}]=J(),[E,{isLoading:$}]=Z(),I=S.length>0;t.useEffect(()=>{l().then(e=>{e.error?u(e.error):u(null)})},[l]),t.useEffect(()=>{d&&P(d.map(e=>({id:e.id??0,tons:e.tons??0,sqft:Number(e.sqft)||0})))},[d]);const f=t.useCallback(e=>{s(e||v),D(!!e),a({}),g(!0)},[]),p=t.useCallback(()=>{s(v),a({}),g(!1)},[]),k=t.useCallback(e=>{const{name:c,value:h}=e.target,T=parseFloat(h)||0;s(C=>({...C,[c]:T})),T>0&&a(C=>({...C,[c]:""}))},[]),x=t.useCallback(()=>{const e={};return(!n.tons||n.tons<=0)&&(e.tons="Enter valid Ton value"),(!n.sqft||n.sqft<=0)&&(e.sqft="Enter valid Sqft value"),a(e),Object.keys(e).length===0},[n]),O=t.useCallback(async()=>{if(!x())return;const e={tons:n.tons,sqft:n.sqft};try{r?(await E({stoneConversion:{...e,id:n.id,sqft:n.sqft.toString()}}).unwrap(),b("Stone Conversion updated successfully","success")):(await q({stoneConversion:{...e,sqft:n.sqft.toString()}}).unwrap(),b("Stone Conversion created successfully","success")),p(),l()}catch(c){u(c),a(h=>({...h,submit:"Failed to save. Please try again."}))}},[n,r,E,q,p,l,x]),W=t.useMemo(()=>[{field:"tons",headerName:"Tons",flex:1},{field:"sqft",headerName:"Sqft",flex:1},{field:"actions",headerName:"Actions",flex:1,sortable:!1,renderCell:e=>o(M,{type:"Edit",onClick:()=>f(e.row)})}],[f]);return o(Y,{error:j||y,children:m(V,{children:[m(R,{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2,children:[o(w,{variant:"h6",fontWeight:600,children:"Stone Conversion Configuration"}),o(U,{btnType:"create",onClick:()=>f(),disabled:I,children:"Add Conversion"})]}),y&&o(w,{color:"error",mb:2,children:"Failed to load data. Please refresh."}),o(B.Suspense,{fallback:o("div",{children:"Loading table..."}),children:o(K,{rows:S,columns:W,getRowId:e=>e.id,loading:F,disableRowSelectionOnClick:!0,autoPagination:"client"})}),o(Q,{title:r?"Edit Conversion":"Add Conversion",open:L,onClose:p,onConfirm:O,maxWidth:"xs",cancelText:"Cancel",confirmText:r?"Update":"Submit",children:m(z,{children:[o(A,{fullWidth:!0,margin:"normal",label:"Tons",name:"tons",type:"number",onKeyDown:e=>{["e","E","+","-"].includes(e.key)&&e.preventDefault()},onWheel:e=>{e.target.blur()},inputProps:{min:1},value:n.tons,onChange:k,error:!!i.tons,helperText:i.tons}),o(A,{fullWidth:!0,margin:"normal",label:"Sqft",name:"sqft",type:"number",onKeyDown:e=>{["e","E","+","-"].includes(e.key)&&e.preventDefault()},onWheel:e=>{e.target.blur()},inputProps:{min:1},value:n.sqft,onChange:k,error:!!i.sqft,helperText:i.sqft})]})})]})})}export{te as default};
