import{ah as w,ce as k,cg as Q,cf as H,aa as M,ch as O,F as x,r as q,e as K,ak as A,h as L,u as W,ac as z,ck as G,cT as J,cU as V,v as X,aB as Y,ct as Z,p as r,E,j as o,ae as N,cm as ee,cn as te,S as se,aN as ae}from"./bundle.CHKSNOJF.js";import{b as ne}from"./blank-invitation-874ut05T.js";import{u as ie,Q as oe,A as ce}from"./useSaveKeyboardShortcut-DqMgB4Vi.js";import{u as re}from"./useInvoiceUtilities-D6VXthvk.js";import"./Banner-DfqOBd6U.js";function ue(u){const h=w(),[e]=k(),{setErrors:l,isDefaultTerms:m,isDefaultFooter:_,setIsFormBusy:p,isFormBusy:i}=u,y=Q(),S=H(),t=M(O);return async j=>{if(i)return;x.processing(),p(!0),l(void 0),await S({excludeToasters:!0});let v="/api/v1/invoices?";m?(v+="save_default_terms=true",_&&(v+="&save_default_footer=true")):_&&(v+="save_default_footer=true"),q("POST",K(v),j).then(async d=>{(m||_)&&await y(),x.success("created_invoice"),A(["products","tasks"]),e.get("action")==="invoice_expense"&&A(["expenses"]),h(L("/invoices/:id/edit?table=:table",{id:d.data.data.id,table:e.get("table")??"products"}))}).catch(d=>{if(d.response?.status===422){const g=d.response.data;g.errors.amount?x.error(g.errors.amount[0]):x.dismiss(),l(g)}}).finally(()=>{t(void 0),p(!1)})}}function _e(){const{t:u}=W(),{documentTitle:h}=z("new_invoice"),[e,l]=G(ae),{data:m,isLoading:_}=J({enabled:typeof e>"u"}),p=V(),i=X(),[y,S]=Y(Z),[t]=k(),[j,v]=r.useState(),[d,g]=r.useState(),[b,B]=r.useState(!1),[D,F]=r.useState(!1),[I,$]=r.useState(!1),P=[{name:u("invoices"),href:"/invoices"},{name:u("new_invoice"),href:"/invoices/create"}],R=[{name:u("create"),href:"/invoices/create"},{name:u("documents"),href:"/invoices/create/documents"},{name:u("settings"),href:"/invoices/create/settings"}],{handleChange:f,calculateInvoiceSum:U}=re({client:d}),T=ue({setErrors:v,isDefaultTerms:D,isDefaultFooter:I,isFormBusy:b,setIsFormBusy:B});r.useEffect(()=>(S(void 0),l(a=>{let s=a;if(t.get("action")!=="clone"&&t.get("action")!=="invoice_project"&&t.get("action")!=="invoice_task"&&t.get("action")!=="invoice_expense"&&t.get("action")!=="invoice_product"&&t.get("action")!=="invoice_transaction"&&(s=void 0),typeof m<"u"&&typeof s>"u"&&t.get("action")!=="clone"){const n=E.cloneDeep(m);typeof n.line_items=="string"&&(n.line_items=[]),t.get("client")&&(n.client_id=t.get("client")),n.uses_inclusive_taxes=i?.settings?.inclusive_taxes??!1,s=n}return s}),()=>{t.get("action")!=="clone"&&t.get("action")!=="invoice_project"&&t.get("action")!=="invoice_task"&&t.get("action")!=="invoice_expense"&&t.get("action")!=="invoice_product"&&t.get("action")!=="invoice_transaction"&&l(void 0)}),[m]);const C=(a,s)=>a?.settings?.[`tax_name${s}`]?{name:a.settings[`tax_name${s}`],rate:a.settings[`tax_rate${s}`]}:a?.group_settings?.settings?.[`tax_name${s}`]?{name:a?.group_settings?.settings[`tax_name${s}`],rate:a?.group_settings?.settings[`tax_rate${s}`]}:{name:i?.settings[`tax_name${s}`],rate:i?.settings[`tax_rate${s}`]};return r.useEffect(()=>{e&&e.client_id.length>1&&p.find(e.client_id).then(a=>{g(a);const s=[];if(a.contacts.map(n=>{if(n.send_email){const c=E.cloneDeep(ne);c.client_contact_id=n.id,c.can_sign=n.can_sign,s.push(c)}}),f("invitations",s),!a.is_tax_exempt){if(i&&i.enabled_tax_rates>0&&t.get("action")!=="clone"){const{name:n,rate:c}=C(a,"1");f("tax_name1",n),f("tax_rate1",c)}if(i&&i.enabled_tax_rates>1&&t.get("action")!=="clone"){const{name:n,rate:c}=C(a,"2");f("tax_name2",n),f("tax_rate2",c)}if(i&&i.enabled_tax_rates>2&&t.get("action")!=="clone"){const{name:n,rate:c}=C(a,"3");f("tax_name3",n),f("tax_rate3",c)}}})},[e?.client_id]),r.useEffect(()=>{e&&U(e)},[e]),ie({isEnabled:!!(e&&e.client_id.length>0&&!b),onSave:()=>T(e)}),o.jsxs(o.Fragment,{children:[o.jsx(N,{title:h,breadcrumbs:P,onSaveClick:()=>T(e),disableSaveButton:e?.client_id.length===0||b,aboveMainContainer:e&&o.jsx(oe,{resource:e}),children:_?o.jsx("div",{className:"flex justify-center items-center",children:o.jsx(se,{})}):o.jsxs("div",{className:"space-y-4",children:[o.jsx(ee,{tabs:R}),o.jsx(te,{context:{invoice:e,setInvoice:l,errors:j,isDefaultTerms:D,setIsDefaultTerms:F,isDefaultFooter:I,setIsDefaultFooter:$,client:d,invoiceSum:y}})]})}),o.jsx(ce,{invoice:e,setInvoice:l})]})}export{_e as default};
