import{a$ as re,Q as q,w as ne,x as oe,r as o,u as G,bX as Ie,y as Ce,H as X,e,D as we,f as Ue,bY as Se,g as De,B as ie,T as Re,bZ as Ee,aH as Ae,b0 as Oe,i as Be,k as Te,F as le,l as Ne,b_ as We,b$ as Me,c0 as ce,A as U,G as S,J as de,K as pe,P as M,N as Pe,n as Le,aD as $e,U as Fe,M as Ve,E as ze,S as Z,V as He,o as _e,p as J,bQ as qe,ar as Ge,as as Ye,at as Qe,aO as Xe,av as Ze,bm as a,bR as K,au as Je,bl as _,t as u,aN as P,bn as Ke,a7 as ee,b8 as se,c1 as es,c2 as ae}from"./index-_6SmYgWK.js";/**
 * @license @tabler/icons-react v3.31.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var ss=re("outline","unlink","IconUnlink",[["path",{d:"M17 22v-2",key:"svg-0"}],["path",{d:"M9 15l6 -6",key:"svg-1"}],["path",{d:"M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464",key:"svg-2"}],["path",{d:"M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463",key:"svg-3"}],["path",{d:"M20 17h2",key:"svg-4"}],["path",{d:"M2 7h2",key:"svg-5"}],["path",{d:"M7 2v2",key:"svg-6"}]]);/**
 * @license @tabler/icons-react v3.31.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var te=re("outline","user-plus","IconUserPlus",[["path",{d:"M8 7a4 4 0 1 0 8 0a4 4 0 0 0 -8 0",key:"svg-0"}],["path",{d:"M16 19h6",key:"svg-1"}],["path",{d:"M19 16v6",key:"svg-2"}],["path",{d:"M6 21v-2a4 4 0 0 1 4 -4h4",key:"svg-3"}]]);const as=Ae(We)({boxShadow:"0px 8px 10px -5px rgb(0 0 0 / 20%), 0px 16px 24px 2px rgb(0 0 0 / 14%), 0px 6px 30px 5px rgb(0 0 0 / 12%)",borderRadius:"10px",[`& .${Me.listbox}`]:{boxSizing:"border-box","& ul":{padding:10,margin:10}}}),ue=({show:j,dialogProps:d,onCancel:L,onConfirm:D})=>{const R=document.getElementById("portal"),b=q(t=>t.auth.user),g=ne();oe();const I=(...t)=>g(de(...t)),E=(...t)=>g(pe(...t)),[y,A]=o.useState(""),[p,O]=o.useState({}),[B,$]=o.useState([]),[i,f]=o.useState(""),k=G(Ie.getAllRolesByOrganizationId);o.useEffect(()=>{if(k.data){const t=k.data.map(l=>({id:l.id,name:l.name,label:l.name,description:l.description}));if($(t),d.type==="EDIT"&&d.data&&d.data.role&&d.data.role.name){const l=t.find(C=>C.name===d.data.role.name);l&&f(l)}}},[k.data]),o.useEffect(()=>(d.data&&(k.request(b.activeOrganizationId),O(d.data.user),A(d.data.user.email)),()=>{A(""),O({})}),[d]),o.useEffect(()=>(g(j?{type:Ce}:{type:X}),()=>g({type:X})),[j,g]);const v=async()=>{try{const t={userId:p.id,workspaceId:d.data.workspaceId,organizationId:b.activeOrganizationId,roleId:i.id,updatedBy:b.id},l=await ce.updateWorkspaceUserRole(t);l.data&&(I({message:"WorkspaceUser Details Updated",options:{key:new Date().getTime()+Math.random(),variant:"success",action:C=>e.jsx(U,{style:{color:"white"},onClick:()=>E(C),children:e.jsx(S,{})})}}),D(l.data.id))}catch(t){I({message:`Failed to update WorkspaceUser: ${typeof t.response.data=="object"?t.response.data.message:t.response.data}`,options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:l=>e.jsx(U,{style:{color:"white"},onClick:()=>E(l),children:e.jsx(S,{})})}})}},F=(t,l)=>{f(l)},T=j?e.jsxs(we,{fullWidth:!0,maxWidth:"sm",open:j,onClose:L,"aria-labelledby":"alert-dialog-title","aria-describedby":"alert-dialog-description",children:[e.jsx(Ue,{sx:{fontSize:"1rem"},id:"alert-dialog-title",children:e.jsxs("div",{style:{display:"flex",flexDirection:"row",alignItems:"center"},children:[e.jsx(Se,{style:{marginRight:"10px"}}),"Change Workspace Role - "," ",y||""," ",p.name?`(${p.name})`:""]})}),e.jsx(De,{children:e.jsxs(ie,{sx:{p:1},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"row"},children:[e.jsxs(Re,{children:["New Role to Assign",e.jsx("span",{style:{color:"red"},children:" *"})]}),e.jsx("div",{style:{flexGrow:1}})]}),e.jsx(Ee,{size:"small",sx:{mt:1},onChange:F,getOptionLabel:t=>t.label||"",options:B,renderInput:t=>e.jsx(Oe,{...t,variant:"outlined",placeholder:"Select Role"}),value:i,PopperComponent:as})]})}),e.jsx(Be,{sx:{px:3,pb:2},children:e.jsx(Te,{variant:"contained",onClick:()=>v(),id:"btn_confirmEditUser",children:d.confirmButtonName})}),e.jsx(le,{})]}):null;return Ne.createPortal(T,R)};ue.propTypes={show:M.bool,dialogProps:M.object,onCancel:M.func,onConfirm:M.func};const rs=()=>{const j=Pe(),d=q(s=>s.customization),L=q(s=>s.auth.user),D=Le(),R=ne();oe();const{error:b,setError:g}=$e(),[I,E]=o.useState(""),[y,A]=o.useState({}),[p,O]=o.useState([]),[B,$]=o.useState(!0),[i,f]=o.useState([]),[k,v]=o.useState(!1),[F,T]=o.useState({}),[t,l]=o.useState(!1),[C,xe]=o.useState({}),V=(...s)=>R(de(...s)),z=(...s)=>R(pe(...s)),{confirm:he}=Fe(),x=G(ae.getAllUsersByWorkspaceId),N=G(ce.getWorkspaceById),W=document.location.pathname.toString().split("/"),w=W[W.length-1]==="workspace-users"?"":W[W.length-1],ge=s=>{if(s.target.checked){const r=(p||[]).filter(n=>!n.isOrgOwner).map(n=>({userId:n.userId,name:n.user.name,email:n.user.email}));f(r);return}f([])},me=(s,r)=>{const n=i.findIndex(c=>c.userId===r.userId);let h=[];n===-1?h=h.concat(i,{userId:r.userId,name:r.user.name,email:r.user.email}):n===0?h=h.concat(i.slice(1)):n===i.length-1?h=h.concat(i.slice(0,-1)):n>0&&(h=h.concat(i.slice(0,n),i.slice(n+1))),f(h)},je=s=>i.findIndex(r=>r.userId===s)!==-1,Y=()=>{T({type:"ADD",cancelButtonName:"Cancel",confirmButtonName:"Send Invite",data:y}),v(!0)},Q=s=>{s.status.toUpperCase()==="INVITED"?fe(s):ke(s)},fe=s=>{const r={type:"EDIT",cancelButtonName:"Cancel",confirmButtonName:"Update Invite",data:{...s,isWorkspaceUser:!0},disableWorkspaceSelection:!0};T(r),v(!0)},ke=s=>{const n={type:"EDIT",cancelButtonName:"Cancel",confirmButtonName:"Update Role",data:{...s,assignedRoles:[{role:s.role,active:!0}],workspaceId:w}};xe(n),l(!0)},be=async()=>{const r={title:"Remove Users",description:`Remove the following users from the workspace?
${i.map(c=>c.name?`${c.name} (${c.email})`:c.email).join(", ")}`,confirmButtonName:"Remove",cancelButtonName:"Cancel"};if(p.find(c=>i.some(m=>m.userId===c.id)&&c.isOrgOwner===!0)){V({message:"Organization owner cannot be removed from workspace.",options:{key:new Date().getTime()+Math.random(),variant:"error",action:c=>e.jsx(U,{style:{color:"white"},onClick:()=>z(c),children:e.jsx(S,{})})}});return}if(await he(r)){try{const c=i.map(m=>ae.deleteWorkspaceUser(w,m.userId));if(await Promise.all(c),V({message:`${i.length} User(s) removed from workspace.`,options:{key:new Date().getTime()+Math.random(),variant:"success",action:m=>e.jsx(U,{style:{color:"white"},onClick:()=>z(m),children:e.jsx(S,{})})}}),i.some(m=>m.userId===L.id)){D("/",{replace:!0}),D(0);return}H()}catch(c){V({message:`Failed to unlink users: ${typeof c.response.data=="object"?c.response.data.message:c.response.data}`,options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:m=>e.jsx(U,{style:{color:"white"},onClick:()=>z(m),children:e.jsx(S,{})})}})}f([])}},H=()=>{v(!1),l(!1),x.request(w)},ye=s=>{E(s.target.value)};function ve(s){var r,n;return((r=s.user.name)==null?void 0:r.toLowerCase().indexOf(I.toLowerCase()))>-1||((n=s.user.email)==null?void 0:n.toLowerCase().indexOf(I.toLowerCase()))>-1}return o.useEffect(()=>{N.request(w),x.request(w)},[]),o.useEffect(()=>{N.data&&A(N.data)},[N.data]),o.useEffect(()=>{if(x.data){const s=x.data||[],r=s.find(n=>n.isOrgOwner);r&&(s.splice(s.indexOf(r),1),s.unshift(r)),O(s)}},[x.data]),o.useEffect(()=>{x.error&&g(x.error)},[x.error,g]),o.useEffect(()=>{$(x.loading)},[x.loading]),e.jsxs(e.Fragment,{children:[e.jsx(Ve,{children:b?e.jsx(ze,{error:b}):e.jsxs(Z,{flexDirection:"column",sx:{gap:3},children:[e.jsx(He,{isBackButton:!0,isEditButton:!1,onBack:()=>window.history.back(),search:p.length>0,onSearchChange:ye,searchPlaceholder:"Search Users",title:((y==null?void 0:y.name)||"")+": Workspace Users",description:"Manage workspace users and permissions.",children:p.length>0&&e.jsxs(e.Fragment,{children:[e.jsx(_e,{permissionId:"workspace:unlink-user",sx:{borderRadius:2,height:"100%"},variant:"outlined",disabled:i.length===0,onClick:be,color:"error",startIcon:e.jsx(ss,{}),children:"Remove Users"}),e.jsx(J,{permissionId:"workspace:add-user",variant:"contained",sx:{borderRadius:2,height:"100%"},onClick:Y,startIcon:e.jsx(te,{}),children:"Add User"})]})}),!B&&(p==null?void 0:p.length)<=0?e.jsxs(Z,{sx:{alignItems:"center",justifyContent:"center"},flexDirection:"column",children:[e.jsx(ie,{sx:{p:2,height:"auto"},children:e.jsx("img",{style:{objectFit:"cover",height:"20vh",width:"auto"},src:qe,alt:"empty_datasetSVG"})}),e.jsx("div",{children:"No Assigned Users Yet"}),e.jsx(J,{permissionId:"workspace:add-user",variant:"contained",sx:{borderRadius:2,height:"100%",mt:2,color:"white"},startIcon:e.jsx(te,{}),onClick:Y,children:"Add User"})]}):e.jsx(e.Fragment,{children:e.jsx(Ge,{sx:{border:1,borderColor:j.palette.grey[900]+25,borderRadius:2},component:Ye,children:e.jsxs(Qe,{sx:{minWidth:650},"aria-label":"simple table",children:[e.jsx(Xe,{sx:{backgroundColor:d.isDarkMode?j.palette.common.black:j.palette.grey[100],height:56},children:e.jsxs(Ze,{children:[e.jsx(a,{padding:"checkbox",children:e.jsx(K,{color:"primary",checked:i.length===(p||[]).length-1,onChange:ge,inputProps:{"aria-label":"select all"}})}),e.jsx(a,{children:"Email/Name"}),e.jsx(a,{children:"Role"}),e.jsx(a,{children:"Status"}),e.jsx(a,{children:"Last Login"}),e.jsx(a,{children:" "})]})}),e.jsx(Je,{children:B?e.jsxs(e.Fragment,{children:[e.jsxs(_,{children:[e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})})]}),e.jsxs(_,{children:[e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})}),e.jsx(a,{children:e.jsx(u,{variant:"text"})})]})]}):e.jsx(e.Fragment,{children:(p||[]).filter(ve).map((s,r)=>e.jsxs(_,{hover:!0,sx:{"&:last-child td, &:last-child th":{border:0}},children:[e.jsx(a,{padding:"checkbox",children:s.isOrgOwner?null:e.jsx(K,{color:"primary",checked:je(s.userId),onChange:n=>me(n,s),inputProps:{"aria-labelledby":s.userId}})}),e.jsxs(a,{children:[s.user.name&&e.jsxs(e.Fragment,{children:[s.user.name,e.jsx("br",{})]}),s.user.email]}),e.jsx(a,{children:s.isOrgOwner?e.jsx(P,{size:"small",label:"ORGANIZATION OWNER"}):s.role.name}),e.jsx(a,{children:s.isOrgOwner?e.jsx(e.Fragment,{}):e.jsxs(e.Fragment,{children:[s.status.toUpperCase()==="ACTIVE"&&e.jsx(P,{color:"success",label:s.status.toUpperCase()}),s.status.toUpperCase()==="INVITED"&&e.jsx(P,{color:"warning",label:s.status.toUpperCase()}),s.status.toUpperCase()==="INACTIVE"&&e.jsx(P,{color:"error",label:s.status.toUpperCase()})]})}),e.jsx(a,{children:s.lastLogin?Ke(s.lastLogin).format("DD/MM/YYYY HH:mm"):"Never"}),e.jsxs(a,{children:[!s.isOrgOwner&&s.status.toUpperCase()==="INVITED"&&e.jsx(ee,{title:"Edit",color:"primary",onClick:()=>Q(s),children:e.jsx(se,{})}),!s.isOrgOwner&&s.status.toUpperCase()==="ACTIVE"&&e.jsx(ee,{title:"Change Role",color:"primary",onClick:()=>Q(s),children:e.jsx(se,{})})]})]},r))})})]})})})]})}),k&&e.jsx(es,{show:k,dialogProps:F,onCancel:()=>v(!1),onConfirm:H}),t&&e.jsx(ue,{show:t,dialogProps:C,onCancel:()=>l(!1),onConfirm:H}),e.jsx(le,{})]})};export{rs as default};
