import{l as S,k as x,R as l,j as s,E as f,aO as y,at as j,h as C,i as R,bk as A,bE as I,a9 as c,aC as b}from"./app-BgLa5dit.js";import{I as E}from"./BSRfXcBg.chunk.js";import{T as P}from"./DG2-quVR.chunk.js";import"./CRMfsAcH.chunk.js";function w(i){const d=S(),u=x(),[t,r]=l.useState([]),[m,p]=l.useState(!1),[o,g]=l.useState(!1);async function h(){if(t.length)try{p(!0);const e=t.map(n=>u(b.addSubscriber(i.libraryId,n.id)));await Promise.all(e);const a=t.map(n=>u(b.updateSubscriberPermissions(i.libraryId,n.id,o)));await Promise.all(a),d.success("Successfully added subscribers to the library."),i.closePopup()}catch{d.error("Failed to add subscribers. Please try again.")}finally{p(!1)}}return s.jsxs(f,{title:"Add subscribers to library",closePopup:i.closePopup,children:[s.jsxs("div",{className:"mb-3",children:[s.jsx(P,{renderSuggestionsOnInputFocus:!0,errorStateImageUrl:y.TYPEAHEAD_ERROR,placeholder:"Search by customer name or CVC ID",valueKey:"name",selections:t,activationLength:2,debounceTime:200,onSuggestionSelected:e=>{t.some(a=>a.id===e.id)?r(t.filter(a=>a.id!==e.id)):r([...t,e])},removeSelection:e=>r(t.filter(a=>a.id!==e.id)),getRemoteUrl:e=>`/ajax/system/customers/search?query=${e}&with=organisation,logo`,getInputTokenComponent:e=>s.jsx(E,{selection:e,colourVariant:"ultramarine",onClickRemove:a=>r(t.filter(n=>n.id!==a.id)),hideExpander:!0},e.id),formatRemoteSuggestions:e=>e.data.map(a=>({name:a.name,id:a.id,crmId:a.crmId,organisation:a.organisation,value:a})),getEmptyStateComponent:e=>s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"fst-italic mb-2",children:["Couldn’t find any matches for ",e,"."]}),s.jsx(j,{className:"text-info ms-n2",svg:A,appLink:{application:R.CUSTOMER_WIZARD,action:C.CustomerWizard.CUSTOMER_NAME,params:{name:e}},children:"Add customer"})]}),getSuggestionComponent:e=>s.jsx(s.Fragment,{children:e.name})}),s.jsxs("div",{className:"mt-3",children:[s.jsx(I,{on:o,onClick:()=>g(!o)}),s.jsx("span",{className:"ms-2",children:"Enable library requests"})]})]}),s.jsxs(c,{children:[s.jsx(c.Cancel,{text:"Cancel",onClick:i.closePopup}),s.jsx(c.Submit,{text:"Add subscribers",variant:"dark",onClick:h,showSpinner:m,disabled:!t.length||m})]})]})}export{w as AddLibrarySubscriberView};
//# sourceMappingURL=DRmjPOU9.chunk.js.map
