import{O as I,a8 as K,r as m,j as s,b as r,S as g,q as h,E as L,d as R,ai as W,a0 as A,aF as b,ab as U,aI as D,B as p,T as f,aJ as M,aK as q,a3 as G,o as y,aL as H,aM as J,aN as v,F as P,a2 as Q}from"./index-DlNZgIG9.js";import{d as V}from"./ExpandMore-CXy18wMW.js";import{d as X}from"./ExpandLess-BtkvLBIN.js";import{u as Y}from"./QuotationApi-Cb0PaTwJ.js";const Z=()=>({_key:crypto.randomUUID(),serviceCode:"",serviceName:"",isChargeable:!0,remarks:"",charges:[]}),te=({module:x,services:i,readOnly:o=!1,onServicesChange:d,taxMode:z="UNKNOWN"})=>{const u=I(),[N,{data:S=[]}]=Y(),[w,_]=K.useState(new Set);m.useEffect(()=>{N(x)},[x]);const $=m.useCallback(()=>{d([...i,Z()])},[i,d]),j=m.useCallback(e=>{d(i.filter((a,t)=>t!==e))},[i,d]),n=m.useCallback((e,a)=>{const t=[...i];t[e]={...t[e],...a},d(t)},[i,d]),E=m.useCallback((e,a)=>{if(!a){n(e,{serviceCode:"",serviceName:"",serviceConfigId:void 0});return}n(e,{serviceConfigId:a.id,serviceCode:a.serviceCode,serviceName:a.name,isChargeable:a.defaultChargeable,remarks:a.defaultRemarks??""})},[n]),F=e=>{_(a=>{const t=new Set(a);return t.has(e)?t.delete(e):t.add(e),t})},T=(e,a)=>{var t;return e._key??((t=e.id)==null?void 0:t.toString())??String(a)};return s(p,{children:[s(g,{direction:"row",justifyContent:"space-between",alignItems:"center",sx:{mb:1.5},children:[s(h,{variant:"subtitle1",fontWeight:600,children:["Services",i.length>0&&r(L,{label:i.length,size:"small",color:"primary",sx:{ml:1,height:20,fontSize:11}})]}),!o&&r(W,{variant:"outlined",size:"small",startIcon:r(R,{}),onClick:$,children:"Add Service"})]}),i.length===0&&r(h,{variant:"body2",color:"text.disabled",sx:{py:2},children:'No services added. Click "Add Service" to begin.'}),r(g,{spacing:1.5,children:i.map((e,a)=>{var k;const t=T(e,a),C=w.has(t),B=((k=e.charges)==null?void 0:k.length)??0;return s(A,{variant:"outlined",sx:{p:1.5,borderRadius:1.5,borderColor:b(u.palette.primary.main,.2),bgcolor:b(u.palette.primary.main,.02)},children:[s(g,{direction:{xs:"column",sm:"row"},spacing:1.5,alignItems:"flex-start",children:[r(U,{sx:{flex:1,minWidth:200},size:"small",options:S,getOptionLabel:l=>l.name,value:S.find(l=>l.id===e.serviceConfigId)??null,onChange:(l,c)=>E(a,c),disabled:o,freeSolo:!1,renderInput:l=>r(f,{...l,label:"Service",placeholder:"Select or type service name",size:"small"}),renderOption:(l,c)=>D(p,{component:"li",...l,key:c.id},s(p,{children:[r(h,{variant:"body2",children:c.name}),s(h,{variant:"caption",color:"text.secondary",children:[c.serviceCode,c.description?` — ${c.description}`:""]})]}))}),!e.serviceConfigId&&r(f,{sx:{flex:1,minWidth:160},size:"small",label:"Service Name",value:e.serviceName,onChange:l=>n(a,{serviceName:l.target.value}),disabled:o,placeholder:"Custom service name"}),r(f,{sx:{flex:1.5,minWidth:180},size:"small",label:"Remarks",value:e.remarks??"",onChange:l=>n(a,{remarks:l.target.value}),disabled:o,placeholder:"Optional remarks"}),r(M,{sx:{mx:0,whiteSpace:"nowrap"},control:r(q,{size:"small",checked:e.isChargeable,onChange:l=>n(a,{isChargeable:l.target.checked}),disabled:o}),label:"Chargeable"}),e.isChargeable&&r(G,{title:C?"Hide charges":`${B} charge(s) — click to manage`,children:r(y,{size:"small",onClick:()=>F(t),color:"primary",children:C?r(X,{fontSize:"small"}):r(V,{fontSize:"small"})})}),!o&&r(y,{size:"small",color:"error",onClick:()=>j(a),"aria-label":"Remove service",children:r(H,{fontSize:"small"})})]}),e.isChargeable&&C&&r(p,{sx:{mt:1.5,pl:1.5,borderLeft:`3px solid ${b(u.palette.primary.main,.3)}`},children:r(J,{charges:e.charges??[],onChange:l=>n(a,{charges:l,totalChargeAmount:v(l)}),readOnly:o,taxMode:z,title:"Charges under this service"})})]},t)})}),i.length>0&&s(P,{children:[r(Q,{sx:{mt:2,mb:1}}),r(g,{direction:"row",justifyContent:"flex-end",children:s(h,{variant:"body2",color:"text.secondary",children:["Services charges total:"," ",s("strong",{children:["₹",i.filter(e=>e.isChargeable).reduce((e,a)=>e+v(a.charges??[]),0).toFixed(2)]})]})})]})]})};export{te as S};
