import{_t as e,lt as t,n as r,yt as a}from"./render-cqdoDfd9.js";import{t as o}from"./Box-pvlRBLzs.js";import{t as n}from"./Button-D-nJohaT.js";import{t as s}from"./Alert-BpUkH7Nx.js";import{t as i}from"./DialogActions-IskUXpPi.js";import{t as l}from"./utils-C0ZHLGh_.js";import{t as c}from"./RequiredTextField-P1YoYBsM.js";import{t as m}from"./ImageUpload-D80dIRPq.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,C]=(0,d.useState)(f||""),[k,_]=(0,d.useState)(""),[v,b]=(0,d.useState)(""),[T,U]=(0,d.useState)(null),[O,D]=(0,d.useState)(),{localeMessages:M,apiBaseUrl:q}=r(),w=()=>{let e=!0;return x.trim()?_(""):(_(M.name_required),e=!1),y.trim()?b(""):(b(M.description_required),e=!1),e};return(0,p.jsxs)(o,{p:2,children:[O&&(0,p.jsx)(s,{severity:"error",sx:{mb:2},children:O}),(0,p.jsx)(c,{label:M.name,helperText:k,fullWidth:!0,margin:"normal",value:x,onChange:e=>S(e.target.value)}),(0,p.jsx)(c,{label:M.description,helperText:v,fullWidth:!0,margin:"normal",multiline:!0,rows:4,value:y,onChange:e=>C(e.target.value)}),(0,p.jsx)(m,{initialUrl:g,onUploadSuccess:e=>{U(e.file_path)},onUploadError:e=>{setErrorMessages(M.logo_upload_failed)}}),(0,p.jsxs)(i,{children:[(0,p.jsx)(n,{onClick:a,children:M.cancel}),(0,p.jsx)(n,{variant:"contained",type:"submit",color:"secondary",onClick:u?t=>{t.preventDefault(),w()&&fetch(`${q}/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(M.error_try_again)})}:r=>{if(r.preventDefault(),!w())return;let a={name:x,description:y};T&&(a.logo=T),T||g||(a.remove_logo=!0),fetch(`${q}/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(M.error_try_again),t(e)})},children:u?M.create:M.update})]})]})}export{u as default};