import{j as a,eJ as f,Z as S,c5 as C,d3 as j,al as v,k as I,l as y,R as p,o as R,E as A,aO as O,at as k,h as E,i as P,bk as T,a9 as l}from"./app-BgLa5dit.js";import{I as U}from"./BSRfXcBg.chunk.js";import{T as w}from"./DG2-quVR.chunk.js";import{O as d}from"./DNClpFKN.chunk.js";import"./CRMfsAcH.chunk.js";const N="_customerLogo_1i3je_1",b={customerLogo:N};function L(n){const{name:c,organisation:r,crmId:t,logo:i}=n.customer;return a.jsxs("div",{className:"d-flex flex-no-wrap",children:[a.jsx("div",{className:`${b.customerLogo} me-3`,children:a.jsx(f,{imageUrl:j.createUrl(i==null?void 0:i.url,{size:v.Small}),svg:C,variant:"green",svgSize:S.ExtraLarge})}),a.jsxs("div",{children:[a.jsxs("h4",{className:"font-size-normal fw-normal m-0",children:[c," - ",t]}),a.jsx("p",{className:"text-secondary m-0",children:(r==null?void 0:r.name)||"No organisation"})]})]})}function D(n){const c=I(),r=y(),[t,i]=p.useState([]),h=R(d.getOrganisation(n.id)),[m,u]=p.useState(!1);async function x(){if(t.length)try{u(!0);const s=t.filter(o=>{var g;return!!((g=o.organisation)!=null&&g.id)}).map(o=>c(d.removeFromOrganisation(o.organisation.id,o.id)));await Promise.all(s),await c(d.addToOrganisation(n.id,t.map(o=>o.id))),r.success("Successfully add customers to the organisation.")}catch{r.error("Failed to add customers to the organisation. Please try again.")}finally{u(!1),n.closePopup()}}return a.jsxs(A,{title:"Add customer to organisation",closePopup:n.closePopup,showLoadingSpinner:!h.hasCompleted,children:[a.jsx("div",{className:"mb-3",children:a.jsx(w,{renderSuggestionsOnInputFocus:!0,errorStateImageUrl:O.TYPEAHEAD_ERROR,placeholder:"Search by customer name or CVC ID",valueKey:"name",selections:t,activationLength:2,debounceTime:200,onSuggestionSelected:e=>{t.some(s=>s.id===e.id)?i(t.filter(s=>s.id!==e.id)):i([...t,e])},removeSelection:e=>i(t.filter(s=>s.id!==e.id)),getRemoteUrl:e=>`/ajax/system/customers/search?query=${e}&with=organisation,logo`,getInputTokenComponent:e=>a.jsx(U,{selection:e,colourVariant:"ultramarine",onClickRemove:s=>i(t.filter(o=>o.id!==s.id)),hideExpander:!0},e.id),formatRemoteSuggestions:e=>e.data.map(s=>({name:s.name,id:s.id,crmId:s.crmId,organisation:s.organisation,value:s})),getEmptyStateComponent:e=>a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:"fst-italic mb-2",children:["Couldn’t find any matches for ",e,"."]}),a.jsx(k,{className:"text-info ms-n2",svg:T,appLink:{application:P.CUSTOMER_WIZARD,action:E.CustomerWizard.CUSTOMER_NAME,params:{name:e,hasOrg:!0,orgId:n.id}},children:"Add customer"})]}),getSuggestionComponent:e=>a.jsx(L,{customer:e,imageCdnUrl:ClickView.Config.Urls.ImageAPI})})}),a.jsxs(l,{children:[a.jsx(l.Cancel,{text:"Cancel",onClick:n.closePopup}),a.jsx(l.Submit,{text:"Add customers",variant:"dark",onClick:x,showSpinner:m,disabled:!t.length||m})]})]})}export{D as AddCustomersToOrganisationView};
//# sourceMappingURL=utYa-FTB.chunk.js.map
