import{O as F,a3 as R,r as m,j as s,b as r,S as g,s as h,F as A,d as I,$ as K,W as L,al as b,a6 as U,ao as D,B as p,T as f,ap as H,aq as q,ar as G,o as y,as as M,at as P,au as v,H as Q,Y}from"./index-BiWH3VAt.js";import{d as J}from"./ExpandMore-H_a9UFYa.js";import{d as V}from"./ExpandLess-Cua6w4wi.js";import{u as X}from"./QuotationApi-Dh7M1bXT.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=F(),[N,{data:S=[]}]=X(),[w,_]=R.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]),T=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]),W=e=>{_(a=>{const t=new Set(a);return t.has(e)?t.delete(e):t.add(e),t})},B=(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(A,{label:i.length,size:"small",color:"primary",sx:{ml:1,height:20,fontSize:11}})]}),!o&&r(K,{variant:"outlined",size:"small",startIcon:r(I,{}),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=B(e,a),C=w.has(t),E=((k=e.charges)==null?void 0:k.length)??0;return s(L,{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)=>T(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(H,{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":`${E} charge(s) — click to manage`,children:r(y,{size:"small",onClick:()=>W(t),color:"primary",children:C?r(V,{fontSize:"small"}):r(J,{fontSize:"small"})})}),!o&&r(y,{size:"small",color:"error",onClick:()=>j(a),"aria-label":"Remove service",children:r(M,{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(P,{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(Q,{children:[r(Y,{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};
