import{K as N,r as t,a as R,b as o,I as U,j as m,B as G,s as w,A as M,R as B,e as K,b1 as V,g as z,T as A,U as Q,V as H}from"./index-CYy8zBsj.js";const J=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:X,useLazyGetStoneConversionListQuery:Y,useCheckDuplicateStoneMutation:ne,useUpdateStoneConversionMutation:Z}=J,v={id:0,tons:1,sqft:0};function te(){const[n,s]=t.useState(v),[r,D]=t.useState(!1),[P,g]=t.useState(!1),[i,a]=t.useState({}),[S,L]=t.useState([]),{openSnackbar:b}=R(),[j,u]=t.useState(null),[l,{data:d,isLoading:F,error:y}]=Y(),[q,{isLoading:_}]=X(),[E,{isLoading:$}]=Z(),I=S.length>0;t.useEffect(()=>{l().then(e=>{e.error?u(e.error):u(null)})},[l]),t.useEffect(()=>{d&&L(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(U,{type:"Edit",onClick:()=>f(e.row)})}],[f]);return o(H,{error:j||y,children:m(Q,{children:[m(G,{display:"flex",justifyContent:"space-between",alignItems:"center",mb:2,children:[o(w,{variant:"h6",fontWeight:600,children:"Stone Conversion Configuration"}),o(M,{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(V,{title:r?"Edit Conversion":"Add Conversion",open:P,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};
