import{n as W,r,j as o,B as _}from"./render-BEL5IN7j.js";import{R as T}from"./RequiredTextField-egwBrnFU.js";import{g as C}from"./utils-MPtJ5ynp.js";import{I as X}from"./ImageUpload-D-S9cmVu.js";import{A as J}from"./Alert-DGyy5JNc.js";import{B as j}from"./Button-CGPZXh3G.js";import"./TextField-9E0h2GEt.js";import"./Select-DyY0x5OK.js";import"./formControlState-YO0xWqIm.js";import"./isMuiElement-e1vpiHuY.js";function ee({successCallback:w,failureCallback:s,cancelCallback:I,activeOrganizationId:h,createMode:i,courseId:a}){const{localeMessages:n,apiBaseUrl:d}=W(),[c,g]=r.useState(""),[f,x]=r.useState(""),[u,S]=r.useState(""),[k,P]=r.useState(!1),[l,R]=r.useState(null),[A,v]=r.useState(""),[N,E]=r.useState(""),[V,y]=r.useState(""),[U,p]=r.useState(""),[q,B]=r.useState(null),[m,F]=r.useState(null);r.useEffect(()=>{!i&&a&&fetch(d+"/organizations/"+h+"/courses/"+a+"/",{method:"GET",headers:{"Content-Type":"application/json","X-CSRFToken":C("csrftoken")},credentials:"include"}).then(e=>{if(!e.ok)throw new Error("Network response was not ok");return e.json()}).then(e=>{g(e.title),x(e.slug),S(e.description),B(e.image),F(e.image_path),e.imap_connection_id&&(R(e.imap_connection_id),P(!0))}).catch(e=>{console.error("Error:",e),e&&s(e)})},[i,a]);const H=()=>{let e=!0;return c?v(""):(v(n.title_required_helper_text),e=!1),f?E(""):(E(n.slug_required_helper_text),e=!1),u?y(""):(y(n.description_required_helper_text),e=!1),e},b=()=>{H()&&fetch(d+"/organizations/"+h+"/courses/"+a+"/",{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":C("csrftoken")},credentials:"include",body:JSON.stringify({title:c,description:u,imap_connection_id:l&&k?parseInt(l):null,reset_imap_connection:!k||l==null,image:m||null})}).then(t=>{if(!t.ok&&t.status!=409)throw t.status>=500&&p("Server error occurred. Please try again later."),new Error("Network response was not ok");return t.json()}).then(t=>{t.error?(p(t.error),s(t)):(console.log("Success:",t),w(t))}).catch(t=>{console.error("Error:",t),s(t)})},D=()=>{H()&&fetch(d+"/organizations/"+h+"/courses/",{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":C("csrftoken")},credentials:"include",body:JSON.stringify({title:c,slug:f,description:u,imap_connection_id:l?parseInt(l):null,image:m||null})}).then(t=>{if(!t.ok&&t.status!=409)throw t.status>=500&&p("Server error occurred. Please try again later."),new Error("Network response was not ok");return t.json()}).then(t=>{t.error?(p(t.error),s(t)):(console.log("Success:",t),g(""),x(""),S(""),w(t))}).catch(t=>{console.error("Error:",t),s(t)})};return o.jsxs(_,{p:2,children:[U&&o.jsx(J,{severity:"error",sx:{marginBottom:"10px"},children:U}),o.jsx(T,{label:n.course_title,helperText:A,fullWidth:!0,margin:"normal",value:c,onChange:e=>g(e.target.value)}),o.jsx(T,{label:n.course_slug,helperText:N,fullWidth:!0,margin:"normal",value:f,onChange:e=>x(e.target.value),...i?{}:{disabled:!0}}),o.jsx(T,{label:n.course_description,helperText:V,fullWidth:!0,margin:"normal",multiline:!0,rows:4,value:u,onChange:e=>S(e.target.value)}),o.jsx(_,{children:o.jsx(X,{initialUrl:q,onUploadSuccess:e=>{B(e.file_url),F(e.file_path)}})}),o.jsxs(_,{mt:2,textAlign:"right",children:[o.jsx(j,{onClick:I,sx:{mr:1},children:"Cancel"}),i&&o.jsx(j,{variant:"contained",onClick:()=>D(),sx:{boxShadow:"none"},children:n.create}),!i&&o.jsx(j,{variant:"contained",onClick:()=>b(),sx:{boxShadow:"none"},children:n.update})]})]})}export{ee as default};
//# sourceMappingURL=CourseForm-Dwy5I2EB.js.map
