import{_t as e,lt as t,n as r,yt as a}from"./render-cqdoDfd9.js";import{t as n}from"./Box-pvlRBLzs.js";import{t as s}from"./Button-D-nJohaT.js";import{t as o}from"./Alert-BpUkH7Nx.js";import{t as l}from"./Tooltip-BI9A-Ujn.js";import{t as i}from"./MenuItem-CsbWpzKb.js";import{t as u}from"./utils-C0ZHLGh_.js";import{t as c}from"./RequiredTextField-P1YoYBsM.js";import{t as d}from"./ImageUpload-D80dIRPq.js";var p=a(e(),1),h=t();function g({successCallback:e,failureCallback:t,cancelCallback:a,activeOrganizationId:g,createMode:m,courseId:_}){const{localeMessages:f,apiBaseUrl:x,languageOptions:j=[]}=r(),[S,k]=(0,p.useState)(""),[v,C]=(0,p.useState)(""),[w,T]=(0,p.useState)(""),[b,y]=(0,p.useState)(""),[O,E]=(0,p.useState)(!1),[I,q]=(0,p.useState)(null),[N,P]=(0,p.useState)(""),[W,z]=(0,p.useState)(""),[B,F]=(0,p.useState)(""),[R,U]=(0,p.useState)(""),[J,M]=(0,p.useState)(""),[X,A]=(0,p.useState)(null),[$,D]=(0,p.useState)(null);(0,p.useEffect)(()=>{m&&!b&&j.length>0&&y("en")},[m,b,j]),(0,p.useEffect)(()=>{!m&&_&&fetch(x+"/organizations/"+g+"/courses/"+_+"/",{method:"GET",headers:{"Content-Type":"application/json","X-CSRFToken":u("csrftoken")},credentials:"include"}).then(e=>{if(!e.ok)throw new Error("Network response was not ok");return e.json()}).then(e=>{k(e.title),C(e.slug),T(e.description),y(e.language||""),A(e.image),D(e.image_path),e.imap_connection_id&&(q(e.imap_connection_id),E(!0))}).catch(e=>{e&&t(e)})},[m,_]);const G=()=>{let e=!0;return S?P(""):(P(f.title_required_helper_text),e=!1),v?/^\S+$/.test(v)?z(""):(z(f.slug_no_space),e=!1):(z(f.slug_required_helper_text),e=!1),w?F(""):(F(f.description_required_helper_text),e=!1),b?U(""):(U(f.language_required_helper_text),e=!1),e};return(0,h.jsxs)(n,{p:2,children:[J&&(0,h.jsx)(o,{severity:"error",sx:{marginBottom:"10px"},children:J}),(0,h.jsx)(c,{label:f.course_title,helperText:N,fullWidth:!0,margin:"normal",value:S,onChange:e=>k(e.target.value)}),(0,h.jsx)(l,{title:m?f.slug_tooltip:"",children:(0,h.jsx)(c,{label:f.course_slug,helperText:W,fullWidth:!0,margin:"normal",value:v,onChange:e=>C(e.target.value),inputProps:{pattern:"^\\S+$",title:f.slug_no_space},...m?{}:{disabled:!0}})}),(0,h.jsx)(c,{label:f.course_language,helperText:R,fullWidth:!0,margin:"normal",value:b,onChange:e=>y(e.target.value),select:!0,children:j.map(e=>(0,h.jsx)(i,{value:e.value,children:e.label},e.value))}),(0,h.jsx)(c,{label:f.course_description,helperText:B,fullWidth:!0,margin:"normal",multiline:!0,rows:4,value:w,onChange:e=>T(e.target.value)}),(0,h.jsx)(n,{children:(0,h.jsx)(d,{initialUrl:X,onUploadSuccess:e=>{A(e.file_url),D(e.file_path)}})}),(0,h.jsxs)(n,{mt:2,textAlign:"right",children:[(0,h.jsx)(s,{onClick:a,sx:{mr:1},children:"Cancel"}),m&&(0,h.jsx)(s,{variant:"contained",onClick:()=>{G()&&fetch(x+"/organizations/"+g+"/courses/",{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":u("csrftoken")},credentials:"include",body:JSON.stringify({title:S,slug:v,description:w,language:b,imap_connection_id:I?parseInt(I):null,image:$||null})}).then(e=>{if(!e.ok&&409!=e.status)throw e.status>=500&&M("Server error occurred. Please try again later."),new Error("Network response was not ok");return e.json()}).then(r=>{r.error?(M(r.error),t(r)):(k(""),C(""),T(""),y(j.length>0?j[0].value:""),e(r))}).catch(e=>{t(e)})},sx:{boxShadow:"none"},children:f.create}),!m&&(0,h.jsx)(s,{variant:"contained",onClick:()=>{G()&&fetch(x+"/organizations/"+g+"/courses/"+_+"/",{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":u("csrftoken")},credentials:"include",body:JSON.stringify({title:S,description:w,language:b,imap_connection_id:I&&O?parseInt(I):null,reset_imap_connection:!O||null==I,image:$||null})}).then(e=>{if(!e.ok&&409!=e.status)throw e.status>=500&&M("Server error occurred. Please try again later."),new Error("Network response was not ok");return e.json()}).then(r=>{r.error?(M(r.error),t(r)):e(r)}).catch(e=>{t(e)})},sx:{boxShadow:"none"},children:f.update})]})]})}export{g as default};