(()=>{"use strict";const{createElement:n,render:e,useState:t,useEffect:s,Fragment:o}=wp.element,{select:a,dispatch:i}=wp.data,r=e=>{const[o,i]=t([]);return s((()=>{fetch(`${window.instructor_announcements.api}/instructor_courses`,{method:"post",body:JSON.stringify({token:a("vibebp").getToken()})}).then((n=>n.json())).then((n=>{n.status&&i(n.courses)}))}),[]),n("select",{onChange:n=>{e.update(n.target.value)},value:e.value},n("option",null,window.instructor_announcements.translations.select_course),o.length?o.map((e=>n("option",{value:e.value},e.label))):"")},{createElement:u,render:c,useState:d,useEffect:l,Fragment:_}=wp.element,{select:m,dispatch:p}=wp.data,w=n=>{const[e,t]=d(!0),[s,o]=d(!1),[a,i]=d(!0),[c,w]=d([]),[g,h]=d({message:"",course_id:0,student_type:""}),[v,b]=d(!1);return l((()=>{let e={type:n.type,token:m("vibebp").getToken()};n.course_id&&(h({...g,course_id:n.course_id}),e.course_id=n.course_id),fetch(`${window.instructor_announcements.api}/instructor_announcements/`,{method:"post",body:JSON.stringify(e)}).then((n=>n.ok?n.json():{status:0,message:window.wplms_course_data.translations.error_loading_data})).then((e=>{e.status&&w(e.announcements),t(!1),document.dispatchEvent(new CustomEvent("vibebp_widget_loaded",{detail:{props:n}}))})).catch((n=>{t(!1),console.error("Uh oh, an error!",n),p("vibebp").addNotification({text:window.wplms_course_data.translations.error_loading_data})}))}),[]),u("div",{className:"instructor_announcements"},u("h3",{class:"widget_title"},n.settings.title),e?u("div",{class:"widget_loader"},u("div",null),u("div",null),u("div",null),u("div",null)):u(_,null,u("div",{className:"existing_announcements"},c.length?c.map((e=>u("div",{className:"course_announcement"},u("a",{href:e.course_link},u("span",null,e.course_title)),u("span",{dangerouslySetInnerHTML:{__html:e.announcement}}),u("a",{className:"vicon vicon-close",onClick:()=>{window.confirm(window.instructor_announcements.translations.are_you_sure)&&(e=>{i(!0),fetch(`${window.instructor_announcements.api}/instructor_announcements/remove?post`,{method:"post",body:JSON.stringify({course_id:e.id,token:m("vibebp").getToken(),type:n.type})}).then((n=>n.ok?n.json():{status:0,message:window.wplms_course_data.translations.error_loading_data})).then((n=>{if(n.status){let n=[...c];n.splice(n.findIndex((n=>n.id===e.id)),1),w(n)}i(!1)})).catch((n=>{console.error("Uh oh, an error!",n),p("vibebp").addNotification({text:window.wplms_course_data.translations.error_loading_data})}))})(e)}})))):u("div",{className:"vbp_message"},window.instructor_announcements.translations.no_announcements)),v?u(_,null,u("textarea",{value:g.message,onChange:n=>{h({...g,message:n.target.value})}}),n.course_id?"":u(r,{value:g.course_id,update:n=>{h({...g,course_id:n})}}),u("select",{value:g.student_type,onChange:n=>{h({...g,student_type:n.target.value})}},Object.keys(window.instructor_announcements.student_types).map((n=>u("option",{value:n},window.instructor_announcements.student_types[n])))),u("a",{className:s?"button is-primary is-loading":"button is-primary",onClick:()=>{if(!g.course_id)return void p("vibebp").addNotification({text:window.instructor_announcements.translations.missing_data});let e=[...c];o(!0),fetch(`${window.instructor_announcements.api}/instructor_announcements/submit?post`,{method:"post",body:JSON.stringify({announcement:g,token:m("vibebp").getToken(),type:n.type,user_id:window.instructor_announcements.user_id})}).then((n=>n.ok?n.json():{status:0,message:window.wplms_course_data.translations.error_loading_data})).then((n=>{o(!1),n.status&&(e.unshift(n.announcement),w(e))})).catch((n=>{o(!1),console.error("Uh oh, an error!",n),p("vibebp").addNotification({text:window.wplms_course_data.translations.error_loading_data})}))}},s?"...":window.instructor_announcements.translations.add_announcement)):u("a",{className:"button is-primary",onClick:()=>{b(!0)}},window.instructor_announcements.translations.add_announcement)))};document.addEventListener("wplms_announcement",(n=>{document.querySelector(".wplms_dashboard_instructor_announcements")&&c(u(w,{settings:n.detail.widget.options}),document.querySelector(".wplms_dashboard_instructor_announcements"))}))})();