import{_t as e,lt as r,n as t,yt as o}from"./render-cqdoDfd9.js";import{t as a}from"./Box-pvlRBLzs.js";import{t as s}from"./Typography-8v555lWC.js";import{t as n}from"./Select-BbgK42y1.js";import{t as i}from"./Button-D-nJohaT.js";import{t as l}from"./MenuItem-CsbWpzKb.js";import{n as d,r as u,t as c}from"./TextField-CbQ6hH_9.js";import{t as h}from"./utils-C0ZHLGh_.js";var m=o(e(),1),f=r(),p=({onClose:e,organizationId:r,refreshUsers:o,user:p=null})=>{const{localeMessages:j,apiBaseUrl:_}=t(),[g,x]=(0,m.useState)(p?p.email:""),[y,v]=(0,m.useState)(p?p.role:"viewer"),[S,b]=(0,m.useState)(""),T=()=>{fetch(`${_}/users/get-or-create-by-email/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":h("csrftoken")},body:JSON.stringify({email:g,organization_id:r})}).then(e=>{if(!e.ok)throw new Error("Failed to get or create user by email");return e.json()}).then(t=>{const a=t.id;var s;s=a,fetch(`${_}/organizations/${r}/users/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":h("csrftoken")},body:JSON.stringify({user_id:s,role:y})}).then(e=>{if(!e.ok)throw new Error("Failed to add user to organization");return e.json()}).then(r=>{o(),e()}).catch(e=>{b(j.failed_to_add_user)})}).catch(e=>{b(j.failed_to_get_or_create_user)})},k=()=>{fetch(`${_}/organizations/${r}/users/${p.user_id}/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":h("csrftoken")},body:JSON.stringify({role:y})}).then(e=>{if(!e.ok)throw new Error("Failed to update user role");return e.json()}).then(r=>{o(),e()}).catch(e=>{b(j.failed_to_update_user_role)})};return(0,f.jsxs)(a,{component:"form",onSubmit:e=>{e.preventDefault(),p?k():T()},sx:{p:4,gap:2,display:"flex",flexDirection:"column"},children:[(0,f.jsx)(s,{variant:"h6",children:p?j.change_user_role:j.add_users_to_organization}),(0,f.jsx)(c,{label:j.email,value:g,onChange:e=>x(e.target.value),required:!0,disabled:!!p}),(0,f.jsxs)(u,{fullWidth:!0,children:[(0,f.jsx)(d,{id:"role-label",children:j.role}),(0,f.jsxs)(n,{labelId:"role-label",value:y,label:j.role,onChange:e=>v(e.target.value),children:[(0,f.jsx)(l,{value:"viewer",children:j.viewer}),(0,f.jsx)(l,{value:"editor",children:j.editor}),(0,f.jsx)(l,{value:"admin",children:j.admin})]})]}),S&&(0,f.jsx)(s,{color:"error",children:S}),(0,f.jsx)(i,{type:"submit",variant:"contained",color:"secondary",children:p?j.edit_user:j.add_user})]})};export{p as default};