import{u as z,r,j as s,B as w}from"./render-D1ZqzUbP.js";import{R as d}from"./RequiredTextField-CyIv_v4F.js";import{I as G}from"./ImageUpload-IdvFym3o.js";import{g as k}from"./utils-VIUIl18e.js";import{A as O}from"./Alert-M7jUzTjE.js";import{f as K,M as Q}from"./Base-Qt2-pbvB.js";import{B as E}from"./Button-DJ_uYab0.js";import"./TextField-B6uk33js.js";import"./Select-Ht3nAO4Q.js";import"./formControlState-C1XOL3mQ.js";import"./isMuiElement-BcEQvMml.js";import"./Link-4dUp4u44.js";import"./Grid-BihMtcd8.js";function ce({successCallback:y,failureCallback:l,cancelCallback:q,activeOrganizationId:S,createMode:o,courseId:c}){const{localeMessages:n,apiBaseUrl:_,languageOptions:a=[]}=z(),[p,T]=r.useState(""),[g,C]=r.useState(""),[h,j]=r.useState(""),[i,m]=r.useState(""),[H,A]=r.useState(!1),[u,N]=r.useState(null),[V,P]=r.useState(""),[W,v]=r.useState(""),[D,I]=r.useState(""),[L,U]=r.useState(""),[B,f]=r.useState(""),[X,F]=r.useState(null),[x,R]=r.useState(null);r.useEffect(()=>{o&&!i&&a.length>0&&m("en")},[o,i,a]),r.useEffect(()=>{!o&&c&&fetch(_+"/organizations/"+S+"/courses/"+c+"/",{method:"GET",headers:{"Content-Type":"application/json","X-CSRFToken":k("csrftoken")},credentials:"include"}).then(e=>{if(!e.ok)throw new Error("Network response was not ok");return e.json()}).then(e=>{T(e.title),C(e.slug),j(e.description),m(e.language||""),F(e.image),R(e.image_path),e.imap_connection_id&&(N(e.imap_connection_id),A(!0))}).catch(e=>{console.error("Error:",e),e&&l(e)})},[o,c]);const b=()=>{let e=!0;const t=/^\S+$/;return p?P(""):(P(n.title_required_helper_text),e=!1),g?t.test(g)?v(""):(v(n.slug_no_space),e=!1):(v(n.slug_required_helper_text),e=!1),h?I(""):(I(n.description_required_helper_text),e=!1),i?U(""):(U(n.language_required_helper_text),e=!1),e},J=()=>{b()&&fetch(_+"/organizations/"+S+"/courses/"+c+"/",{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":k("csrftoken")},credentials:"include",body:JSON.stringify({title:p,description:h,language:i,imap_connection_id:u&&H?parseInt(u):null,reset_imap_connection:!H||u==null,image:x||null})}).then(t=>{if(!t.ok&&t.status!=409)throw t.status>=500&&f("Server error occurred. Please try again later."),new Error("Network response was not ok");return t.json()}).then(t=>{t.error?(f(t.error),l(t)):(console.log("Success:",t),y(t))}).catch(t=>{console.error("Error:",t),l(t)})},$=()=>{b()&&fetch(_+"/organizations/"+S+"/courses/",{method:"POST",headers:{"Content-Type":"application/json","X-CSRFToken":k("csrftoken")},credentials:"include",body:JSON.stringify({title:p,slug:g,description:h,language:i,imap_connection_id:u?parseInt(u):null,image:x||null})}).then(t=>{if(!t.ok&&t.status!=409)throw t.status>=500&&f("Server error occurred. Please try again later."),new Error("Network response was not ok");return t.json()}).then(t=>{t.error?(f(t.error),l(t)):(console.log("Success:",t),T(""),C(""),j(""),m(a.length>0?a[0].value:""),y(t))}).catch(t=>{console.error("Error:",t),l(t)})};return s.jsxs(w,{p:2,children:[B&&s.jsx(O,{severity:"error",sx:{marginBottom:"10px"},children:B}),s.jsx(d,{label:n.course_title,helperText:V,fullWidth:!0,margin:"normal",value:p,onChange:e=>T(e.target.value)}),s.jsx(K,{title:o?n.slug_tooltip:"",children:s.jsx(d,{label:n.course_slug,helperText:W,fullWidth:!0,margin:"normal",value:g,onChange:e=>C(e.target.value),inputProps:{pattern:"^\\S+$",title:n.slug_no_space},...o?{}:{disabled:!0}})}),s.jsx(d,{label:n.course_language,helperText:L,fullWidth:!0,margin:"normal",value:i,onChange:e=>m(e.target.value),select:!0,children:a.map(e=>s.jsx(Q,{value:e.value,children:e.label},e.value))}),s.jsx(d,{label:n.course_description,helperText:D,fullWidth:!0,margin:"normal",multiline:!0,rows:4,value:h,onChange:e=>j(e.target.value)}),s.jsx(w,{children:s.jsx(G,{initialUrl:X,onUploadSuccess:e=>{F(e.file_url),R(e.file_path)}})}),s.jsxs(w,{mt:2,textAlign:"right",children:[s.jsx(E,{onClick:q,sx:{mr:1},children:"Cancel"}),o&&s.jsx(E,{variant:"contained",onClick:()=>$(),sx:{boxShadow:"none"},children:n.create}),!o&&s.jsx(E,{variant:"contained",onClick:()=>J(),sx:{boxShadow:"none"},children:n.update})]})]})}export{ce as default};
//# sourceMappingURL=CourseForm-B8Fk7TH9.js.map
