import{j as a,ej as T,eN as N,bE as D,R as c,O as E,Q as I,c_ as B,U as P,l as L,k as A,o as C,es as m,b as R,aL as M,bT as H,a4 as $,g as z,h as O,i as _,G as W,az as q,cD as Q,E as J,a9 as x}from"./app-BgLa5dit.js";import{D as K}from"./C4BcftDO.chunk.js";import{T as G}from"./lZO4aq5x.chunk.js";import{C as X}from"./c3I67AiW.chunk.js";import{C as Y,a as Z}from"./8iu0vnCW.chunk.js";import{C as ee}from"./B20c1N-e.chunk.js";import"./C8_78Eqq.chunk.js";import"./KUnagq9Z.chunk.js";import"./DULWIWQR.chunk.js";import"./CCcwLDvu.chunk.js";import"./DDfKZ_ng.chunk.js";import"./3nM7jLnE.chunk.js";import"./CPXlV0fg.chunk.js";import"./ChrB5FYK.chunk.js";import"./DR2FaZH9.chunk.js";function se({name:s,permissions:o}){const[e,t]=c.useState(!1);return a.jsxs("div",{children:[a.jsxs("div",{className:"d-flex align-items-center gap-2",children:[a.jsx("span",{children:s}),o.length>0&&a.jsx(E,{type:"button",className:"btn btn-link btn-sm p-0 text-secondary",onClick:()=>t(!e),children:a.jsx(I,{svg:e?B:P})})]}),e&&o.length>0&&o.map(r=>a.jsx("small",{className:"text-secondary d-block",children:r}))]})}const v=T();function ae(s,o){return[v.accessor(e=>e,{id:"name",header:"",cell:e=>{const t=e.getValue(),r=N(t.name);return a.jsx(se,{name:t.name,permissions:r})},size:300}),v.accessor(e=>e,{id:"member",header:"",cell:e=>{const t=e.getValue();return a.jsx(D,{on:!!s[t.id],onClick:()=>o(t.id,!s[t.id])})},size:100})]}function re({data:s,formikValues:o,setFieldValue:e}){const t=Math.ceil(s.length/2),r=s.slice(0,t),f=s.slice(t),p={...X,header:"d-none",row:""},l=ae(o,e);return a.jsxs("div",{className:"row",children:[a.jsx("div",{className:"col-6",children:a.jsx(G,{data:r,columns:l,theme:p})}),a.jsx("div",{className:"col-6",children:a.jsx(G,{data:f,columns:l,theme:p})})]})}function b(s,o){return o.some(e=>e.id===s.id)}function te(s){return async()=>{s.setShowSpinner(!0);const o=s.clickviewGroups.map(e=>{const t=b(e,s.userGroups),r=s.formikProps.values[e.id];return r!==t?r?s.addToGroupById(e.id,e.name):s.removeFromGroupById(e.id,e.name):null}).filter(Boolean);try{await Promise.all(o)}catch{s.alerts.error("Failed to update some group memberships.")}s.setShowSpinner(!1),s.setShowPopup(!1)}}function je(s){const[o,e]=c.useState(!1),[t,r]=c.useState(!1),[f,p]=c.useState({}),l=L(),y=A(),d=C(m.userClickViewGroups(s.id)),u=C(m.clickViewGroups(s.id)),h=C(m.user(s.id)),{hasPermissions:U,isLoading:g}=R(),j=h.data&&M.getUserType(h.data)===H.Learner,w=U($.UserGroup);c.useEffect(()=>{g||!h.hasCompleted||(j||!w)&&z.trigger({application:_.CRM_STUDIO,action:O.CrmStudio.USER_OVERVIEW,args:[s.id]})},[h.hasCompleted,g,j,w]),c.useEffect(()=>{if(d.hasCompleted&&u.hasCompleted){const n={};u.data.forEach(i=>{n[i.id]=b(i,d.data)}),p(n)}},[d.hasCompleted,u.hasCompleted]);async function V(n,i){try{e(!0),await y(m.addUserToClickViewGroup(s.id,n)),e(!1),l.success(`Successfully added user to ${i}`)}catch{l.error(`Failed to add user to ${i}`)}finally{e(!1),r(!1)}}async function k(n,i){try{e(!0),await y(m.removeUserFromClickViewGroup(s.id,n)),e(!1),l.success(`Successfully removed user from ${i}`)}catch{l.error(`Failed to remove user from ${i}`)}finally{e(!1),r(!1)}}const S=d.hasCompleted&&u.hasCompleted;return a.jsx(Y,{view:Z.Users,children:a.jsx(ee,{title:"Curator groups",currentTabId:"groups",id:s.id,loading:!S,children:a.jsx(W,{onSubmit:()=>r(!0),initialValues:f,children:n=>{const i=u.data||[];return a.jsxs(q,{className:"d-flex flex-column",children:[a.jsx(K,{heading:"Curator groups",disableCollapse:!0,children:a.jsx(re,{data:i,formikValues:n.values,setFieldValue:n.setFieldValue})}),a.jsx("div",{className:"d-flex justify-content-end",children:a.jsx(Q,{variant:"dark",disabled:!n.dirty,onClick:()=>r(!0),children:"Update groups"})}),t&&a.jsxs(J,{title:"Update groups",closePopup:()=>r(!1),children:[a.jsx("p",{className:"m-0 pb-3",children:"Are you sure you want to save your changes?"}),a.jsxs(x,{children:[a.jsx(x.Cancel,{onClick:()=>r(!1)}),a.jsx(x.Submit,{showSpinner:o,variant:"dark",text:"Update groups",onClick:te({formikProps:n,setShowSpinner:e,setShowPopup:r,clickviewGroups:u.data,userGroups:d.data,addToGroupById:V,removeFromGroupById:k,alerts:l}),disabled:!n.dirty||o||!S})]})]})]})}})})})}export{je as UsersManageCuratorGroupsView};
//# sourceMappingURL=CK6JKoTu.chunk.js.map
