import{u as v,r as d,j as o,B as T,T as g}from"./render-D1ZqzUbP.js";import{g as c}from"./utils-VIUIl18e.js";import{T as k,F as C,I as E}from"./TextField-B6uk33js.js";import{S as F}from"./Select-Ht3nAO4Q.js";import{M as u}from"./Base-Qt2-pbvB.js";import{B as w}from"./Button-DJ_uYab0.js";import"./formControlState-C1XOL3mQ.js";import"./isMuiElement-BcEQvMml.js";import"./Link-4dUp4u44.js";import"./Grid-BihMtcd8.js";const P=({onClose:m,organizationId:n,refreshUsers:h,user:t=null})=>{const{localeMessages:r,apiBaseUrl:i}=v(),[p,_]=d.useState(t?t.email:""),[s,x]=d.useState(t?t.role:"viewer"),[f,l]=d.useState(""),j=e=>{fetch(`${i}/organizations/${n}/users/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":c("csrftoken")},body:JSON.stringify({user_id:e,role:s})}).then(a=>{if(!a.ok)throw new Error("Failed to add user to organization");return a.json()}).then(a=>{h(),m()}).catch(a=>{console.error("Error adding user to organization:",a),l(r.failed_to_add_user)})},y=e=>{e.preventDefault(),t?b():S()},S=()=>{fetch(`${i}/users/get-or-create-by-email/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":c("csrftoken")},body:JSON.stringify({email:p,organization_id:n})}).then(e=>{if(!e.ok)throw new Error("Failed to get or create user by email");return e.json()}).then(e=>{const a=e.id;j(a)}).catch(e=>{console.error("Error getting or creating user:",e),l(r.failed_to_get_or_create_user)})},b=()=>{console.log("Updating user:",t),fetch(`${i}/organizations/${n}/users/${t.user_id}/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":c("csrftoken")},body:JSON.stringify({role:s})}).then(e=>{if(!e.ok)throw new Error("Failed to update user role");return e.json()}).then(e=>{h(),m()}).catch(e=>{console.error("Error updating user role:",e),l(r.failed_to_update_user_role)})};return o.jsxs(T,{component:"form",onSubmit:y,sx:{p:4,gap:2,display:"flex",flexDirection:"column"},children:[o.jsx(g,{variant:"h6",children:t?r.change_user_role:r.add_users_to_organization}),o.jsx(k,{label:r.email,value:p,onChange:e=>_(e.target.value),required:!0,disabled:!!t}),o.jsxs(C,{fullWidth:!0,children:[o.jsx(E,{id:"role-label",children:r.role}),o.jsxs(F,{labelId:"role-label",value:s,label:r.role,onChange:e=>x(e.target.value),children:[o.jsx(u,{value:"viewer",children:r.viewer}),o.jsx(u,{value:"editor",children:r.editor}),o.jsx(u,{value:"admin",children:r.admin})]})]}),f&&o.jsx(g,{color:"error",children:f}),o.jsx(w,{type:"submit",variant:"contained",color:"secondary",children:t?r.edit_user:r.add_user})]})};export{P as default};
//# sourceMappingURL=UserForm-7kWuQ5qN.js.map
