import{ac as F,u as R,ce as w,v as A,dd as N,ck as V,aS as L,gB as Q,p as t,gC as U,E as h,bS as W,j as o,ae as q,cm as z,cn as G,S as H}from"./bundle.CHKSNOJF.js";import{b as J}from"./blank-invitation-874ut05T.js";import{u as K}from"./usePurchaseOrderUtilities-CdSMZmEe.js";function Z(){const{documentTitle:_}=F("new_purchase_order"),[c]=R(),[n]=w(),v=A(),g=N(),S=[{name:c("purchase_orders"),href:"/purchase_orders"},{name:c("new_purchase_order"),href:"/purchase_orders/create"}],x=[{name:c("create"),href:"/purchase_orders/create"},{name:c("documents"),href:"/purchase_orders/create/documents"},{name:c("settings"),href:"/purchase_orders/create/settings"}],[e,i]=V(L),{data:d,isLoading:y}=Q({enabled:typeof e>"u"}),[E,l]=t.useState(),[j,b]=t.useState(),[C,D]=t.useState(!1),[f,I]=t.useState(!1),[m,O]=t.useState(!1),[P,p]=t.useState(),{calculateInvoiceSum:T,handleChange:k}=K({purchaseOrder:e,setPurchaseOrder:i,setInvoiceSum:l}),B=U({setErrors:b,isDefaultTerms:f,isDefaultFooter:m,isFormBusy:C,setIsFormBusy:D});return t.useEffect(()=>(i(u=>{let r=u;if(n.get("action")!=="clone"&&n.get("action")!=="purchase_order_product"&&(r=void 0),typeof d<"u"&&typeof r>"u"&&n.get("action")!=="clone"){const s=h.cloneDeep(d);typeof s.line_items=="string"&&(s.line_items=[]),n.get("vendor")&&(s.vendor_id=n.get("vendor")),s.line_items.forEach(a=>a._id=W()),s.invitations.forEach(a=>a.client_contact_id=a.client_contact_id||""),s.uses_inclusive_taxes=v?.settings?.inclusive_taxes??!1,r=s}return r}),()=>{n.get("action")!=="clone"&&i(void 0)}),[d]),t.useEffect(()=>{e&&e.vendor_id?g.find(e.vendor_id).then(u=>{p(u);const r=[];u.contacts.map(s=>{if(s.send_email){const a=h.cloneDeep(J);a.vendor_contact_id=s.id,a.can_sign=s.can_sign,r.push(a)}}),k("invitations",r)}):p(void 0)},[e?.vendor_id]),t.useEffect(()=>{e&&T(e)},[e]),o.jsx(q,{title:_,breadcrumbs:S,onSaveClick:()=>e&&B(e),children:y?o.jsx("div",{className:"flex justify-center items-center",children:o.jsx(H,{})}):o.jsxs("div",{className:"space-y-4",children:[o.jsx(z,{tabs:x}),o.jsx(G,{context:{vendor:P,purchaseOrder:e,setPurchaseOrder:i,errors:j,isDefaultTerms:f,setIsDefaultTerms:I,isDefaultFooter:m,setIsDefaultFooter:O,invoiceSum:E,setInvoiceSum:l}})]})})}export{Z as default};
