import{_t as e,lt as t,n as r,yt as a}from"./render-C1-Vvny8.js";import{t as o}from"./Box-BbbSuncO.js";import{t as n}from"./Button-DTVoBv0Y.js";import{t as s}from"./Alert-C8jEdKQG.js";import{t as i}from"./DialogActions-CSfNi6Fl.js";import{t as l}from"./utils-CF94S7_6.js";import{t as c}from"./RequiredTextField-BD22n93B.js";import{t as m}from"./ImageUpload-DyQPtHbL.js";var d=a(e(),1),p=t();function u({successCallback:e,failureCallback:t,cancelCallback:a,createMode:u,initialName:h,initialDescription:f,initialLogoUrl:g,organizationId:j}){const[x,S]=(0,d.useState)(h||""),[y,k]=(0,d.useState)(f||""),[C,_]=(0,d.useState)(""),[b,v]=(0,d.useState)(""),[T,U]=(0,d.useState)(null),[B,D]=(0,d.useState)(),{localeMessages:E,apiBaseUrl:M}=r(),O=()=>{let e=!0;return x.trim()?_(""):(_(E.name_required),e=!1),y.trim()?v(""):(v(E.description_required),e=!1),e};return(0,p.jsxs)(o,{p:2,children:[B&&(0,p.jsx)(s,{severity:"error",sx:{mb:2},children:B}),(0,p.jsx)(c,{label:E.name,helperText:C,fullWidth:!0,margin:"normal",value:x,onChange:e=>S(e.target.value)}),(0,p.jsx)(c,{label:E.description,helperText:b,fullWidth:!0,margin:"normal",multiline:!0,rows:4,value:y,onChange:e=>k(e.target.value)}),(0,p.jsx)(m,{initialUrl:g,onUploadSuccess:e=>{U(e.file_path)},onUploadError:e=>{setErrorMessages(E.logo_upload_failed)}}),(0,p.jsxs)(i,{children:[(0,p.jsx)(n,{onClick:a,children:E.cancel}),(0,p.jsx)(n,{variant:"contained",type:"submit",color:"secondary",onClick:u?t=>{t.preventDefault(),O()&&fetch(`${M}/organizations/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":l("csrftoken")},body:JSON.stringify({name:x,description:y,logo:T})}).then(e=>e.ok?e.json():e.json().then(e=>{throw e})).then(t=>{e(t)}).catch(e=>{setErrorMessages(E.error_try_again)})}:r=>{if(r.preventDefault(),!O())return;let a={name:x,description:y};T&&(a.logo=T),T||g||(a.remove_logo=!0),fetch(`${M}/organizations/${j}/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":l("csrftoken")},body:JSON.stringify(a)}).then(e=>e.ok?e.json():e.json().then(e=>{throw e})).then(t=>{e(t)}).catch(e=>{D(E.error_try_again),t(e)})},children:u?E.create:E.update})]})]})}export{u as default};