import{M,r as t,a as N,b as o,I as R,j as m,B as G,s as w,A as U,a3 as B,e as K,by as z,g as Q,T as A,R as V,a7 as H}from"./index-BiWH3VAt.js";const J=M.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:y}=N(),[j,u]=t.useState(null),[l,{data:d,isLoading:F,error:b}]=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(),y("Stone Conversion updated successfully","success")):(await q({stoneConversion:{...e,sqft:n.sqft.toString()}}).unwrap(),y("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(R,{type:"Edit",onClick:()=>f(e.row)})}],[f]);return o(H,{error:j||b,children:m(V,{children:[m(G,{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"})]}),b&&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(z,{title:r?"Edit Conversion":"Add Conversion",open:P,onClose:p,onConfirm:O,maxWidth:"xs",cancelText:"Cancel",confirmText:r?"Update":"Submit",children:m(Q,{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};
