import{r as n,u as A,j as a,B as H}from"./render-D1ZqzUbP.js";import{R as x}from"./RequiredTextField-CyIv_v4F.js";import{I as M}from"./ImageUpload-IdvFym3o.js";import{g as j}from"./utils-VIUIl18e.js";import{A as N}from"./Alert-M7jUzTjE.js";import{D as P}from"./DialogActions-JiHQNj-t.js";import{B as S}from"./Button-DJ_uYab0.js";import"./TextField-B6uk33js.js";import"./Select-Ht3nAO4Q.js";import"./formControlState-C1XOL3mQ.js";import"./isMuiElement-BcEQvMml.js";function Q({successCallback:p,failureCallback:v,cancelCallback:T,createMode:c,initialName:y,initialDescription:C,initialLogoUrl:u,organizationId:_}){const[s,k]=n.useState(y||""),[i,D]=n.useState(C||""),[E,m]=n.useState(""),[b,d]=n.useState(""),[l,B]=n.useState(null),[h,F]=n.useState(),{localeMessages:t,apiBaseUrl:f}=A(),g=()=>{let e=!0;return s.trim()?m(""):(m(t.name_required),e=!1),i.trim()?d(""):(d(t.description_required),e=!1),e},O=()=>e=>{if(e.preventDefault(),!g())return;let r={name:s,description:i};l&&(r.logo=l),!l&&!u&&(r.remove_logo=!0),fetch(`${f}/organizations/${_}/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":j("csrftoken")},body:JSON.stringify(r)}).then(o=>o.ok?o.json():o.json().then(U=>{throw U})).then(o=>{p(o)}).catch(o=>{F(t.error_try_again),v(o)})},R=()=>e=>{e.preventDefault(),g()&&fetch(`${f}/organizations/`,{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":j("csrftoken")},body:JSON.stringify({name:s,description:i,logo:l})}).then(r=>r.ok?r.json():r.json().then(o=>{throw o})).then(r=>{p(r)}).catch(r=>{setErrorMessages(t.error_try_again)})};return a.jsxs(H,{p:2,children:[h&&a.jsx(N,{severity:"error",sx:{mb:2},children:h}),a.jsx(x,{label:t.name,helperText:E,fullWidth:!0,margin:"normal",value:s,onChange:e=>k(e.target.value)}),a.jsx(x,{label:t.description,helperText:b,fullWidth:!0,margin:"normal",multiline:!0,rows:4,value:i,onChange:e=>D(e.target.value)}),a.jsx(M,{initialUrl:u,onUploadSuccess:e=>{B(e.file_path)},onUploadError:e=>{setErrorMessages(t.logo_upload_failed)}}),a.jsxs(P,{children:[a.jsx(S,{onClick:T,children:t.cancel}),a.jsx(S,{variant:"contained",type:"submit",color:"secondary",onClick:c?R():O(),children:c?t.create:t.update})]})]})}export{Q as default};
//# sourceMappingURL=OrganizationForm-pGYtGO5R.js.map
