import{ac as D,u as T,ce as w,aB as g,e$ as Y,aT as O,f0 as P,p as u,v as F,f1 as U,cU as L,e_ as Q,E as p,i as V,j as i,I as h,aP as q,ek as z,ae as G,cm as H,cn as J,S as K}from"./bundle.CHKSNOJF.js";import{b as W}from"./blank-invitation-874ut05T.js";import{c as X,C as Z}from"./ConfirmActionModal-CPA1AasW.js";function ne(){const{documentTitle:C}=D("new_recurring_invoice"),[c]=T(),[o]=w(),[S]=g(Y),[s,_]=g(O),{data:d,isLoading:j}=P({enabled:typeof s>"u"}),y=[{name:c("recurring_invoices"),href:"/recurring_invoices"},{name:c("new_recurring_invoice"),href:"/recurring_invoices/create"}],I=[{name:c("create"),href:"/recurring_invoices/create"},{name:c("documents"),href:"/recurring_invoices/create/documents"},{name:c("settings"),href:"/recurring_invoices/create/settings"}],[v,b]=u.useState(),[$,E]=u.useState(),[x,k]=u.useState(!1),a=F(),m=U({setErrors:E,setIsFormBusy:k,isFormBusy:x}),R=L(),{handleChange:l,calculateInvoiceSum:A}=Q({client:v}),f=(n,t)=>n?.settings?.[`tax_name${t}`]?{name:n.settings[`tax_name${t}`],rate:n.settings[`tax_rate${t}`]}:n?.group_settings?.settings?.[`tax_name${t}`]?{name:n?.group_settings?.settings[`tax_name${t}`],rate:n?.group_settings?.settings[`tax_rate${t}`]}:{name:a?.settings[`tax_name${t}`],rate:a?.settings[`tax_rate${t}`]};u.useEffect(()=>(_(n=>{let t=n;if(o.get("action")!=="clone"&&(t=void 0),typeof d<"u"&&typeof t>"u"&&o.get("action")!=="clone"){const e=p.cloneDeep(d);typeof e.line_items=="string"&&(e.line_items=[]),o.get("client")&&(e.client_id=o.get("client")),e.next_send_date===""&&(e.next_send_date=V().format("YYYY-MM-DD")),e.uses_inclusive_taxes=a?.settings?.inclusive_taxes??!1,e.auto_bill=a?.settings?.auto_bill??"off",t=e}return t}),()=>{o.get("action")!=="clone"&&_(void 0)}),[d]),u.useEffect(()=>{s&&A(s)},[s]),u.useEffect(()=>{s&&s.client_id.length>1&&R.find(s.client_id).then(n=>{b(n);const t=[];if(n.contacts.map(e=>{if(e.send_email){const r=p.cloneDeep(W);r.client_contact_id=e.id,t.push(r)}}),l("invitations",t),!n.is_tax_exempt){if(a&&a.enabled_tax_rates>0&&o.get("action")!=="clone"){const{name:e,rate:r}=f(n,"1");l("tax_name1",e),l("tax_rate1",r)}if(a&&a.enabled_tax_rates>1&&o.get("action")!=="clone"){const{name:e,rate:r}=f(n,"2");l("tax_name2",e),l("tax_rate2",r)}if(a&&a.enabled_tax_rates>2&&o.get("action")!=="clone"){const{name:e,rate:r}=f(n,"3");l("tax_name3",e),l("tax_rate3",r)}}})},[s?.client_id]);const[,M]=g(X),B=[{onClick:()=>M(!0),label:c("send_now"),icon:i.jsx(h,{element:q})},{onClick:()=>m(s,"start"),label:c("start"),icon:i.jsx(h,{element:z})}];return i.jsxs(G,{title:C,breadcrumbs:y,disableSaveButton:!s?.client_id||x,onSaveClick:()=>m(s),additionalSaveOptions:B,children:[j?i.jsx("div",{className:"flex justify-center items-center",children:i.jsx(K,{})}):i.jsxs("div",{className:"space-y-4",children:[i.jsx(H,{tabs:I}),i.jsx(J,{context:{recurringInvoice:s,setRecurringInvoice:_,errors:$,client:v,invoiceSum:S}})]}),i.jsx(Z,{onClick:()=>m(s,"send_now")})]})}export{ne as default};
