(()=>{const{createElement:t,render:e,useState:s,useEffect:n,Fragment:a}=wp.element,{select:o,dispatch:i}=wp.data,r=e=>{console.log(e);const a=window.contact_users,[r,l]=s(""),d=function(t,e){const[a,o]=s(t);return n((()=>{const e=setTimeout((()=>{o(t)}),500);return()=>{clearTimeout(e)}}),[t,500]),a}(r),[c,u]=s(!1),[_,m]=s(!0),[p,g]=s({friends_active:!1,user_can_edit_post:!1,disable_instructor_display:!0}),[h,v]=s([]),[w,b]=s([]),[f,y]=s([]),[N,x]=s({subject:"",message:""}),[k,j]=s(""),[C,S]=s(!1);n((()=>{fetch(`${a.api}/check?user_id=${e.settings.user_id}&force`,{method:"post",body:JSON.stringify({user_id:e.settings.user_id,token:o("vibebp").getToken()})}).then((t=>t.ok?t.json():{status:0,message:window.wplms_course_data.translations.error_loading_data})).then((t=>{t.status&&g(t.data),m(!1),document.dispatchEvent(new CustomEvent("vibebp_widget_loaded",{detail:{props:e}}))})).catch((t=>{m(!1),document.dispatchEvent(new CustomEvent("vibebp_widget_loaded",{detail:{props:e}})),i("vibebp").addNotification({text:window.wplms_course_data.translations.error_loading_data})})),e.settings.hasOwnProperty("show_instructors_only")&&e.settings.show_instructors_only&&E("get_instructors")}),[]),n((()=>{d&&(S(!0),fetch(`${a.api}/users?force`,{method:"post",body:JSON.stringify({type:k,s:d,user_id:e.settings.user_id,token:o("vibebp").getToken()})}).then((t=>t.json())).then((t=>{t.status&&b(t.data),S(!1)})))}),[d]);const E=t=>{let e="";e="object"==typeof t?t.target.value:t,j(e),v([]),b([])};return t("div",{className:"contact_user"},_?t("div",null,t("div",{className:"widget_loader"},t("div",null),t("div",null),t("div",null),t("div",null))):t("div",{className:"dash-content-form"},t("h3",{class:"widget_title"},e.settings.title),e.settings.hasOwnProperty("show_instructors_only")&&e.settings.show_instructors_only?t("h4",{style:{margin:"1rem 0"}},window.contact_users.translations.select_instructor):t("select",{className:"usergroup-dropdown chosen","data-placeholder":a.translations.select_user_groups,onChange:E},";",t("option",{value:""},a.translations.select_a_user_groups),p.friends_active?t("option",{value:"get_friends"},a.translations.friends):"",p.disable_instructor_display?"":t("option",{value:"get_instructors"},a.translations.instructor),t("option",{value:"get_admins"},a.translations.administrator),p.user_can_edit_post?t("option",{value:"get_course_students"},a.translations.course_students):""),f&&f.length?t("div",{className:"contact_users"},f.map(((e,s)=>t("span",{onClick:()=>{(t=>{let e=f.findIndex((e=>e.id==t.id));if(e>-1){let t=[...f];t.splice(e,1),y(t)}})(e)}},t("span",null,e.name),t("span",null,"❌"))))):"",t("input",{type:"text",name:"to",className:"input-text to usergroup-filter",placeholder:a.translations.type_name_to_autocomplete,onChange:t=>{let e=t.target.value;l(e)}}),C?t("div",{class:"lds-ellipsis"},t("div",null),t("div",null),t("div",null),t("div",null)):w&&w.length?t("div",{className:"contact_users"},w.map(((e,s)=>t("div",{className:"logo",style:{width:"32px"},onClick:()=>(t=>{-1==f.findIndex((e=>e.id==t.id))&&y([...f,t])})(e)},t("h3",{dangerouslySetInnerHTML:{__html:e.pic}}),t("span",null,e.name))))):"",t("input",{type:"text",name:"subject",className:"input-text subject",placeholder:a.translations.enter_subject,onChange:t=>x({...N,subject:t.target.value})}),t("textarea",{name:"message",className:"form_message",placeholder:a.translations.enter_message,onChange:t=>x({...N,message:t.target.value})}),f.length?t("a",{id:"dash_contact_form_submit",className:"button "+(c?"is-loading":""),onClick:t=>{let s=f.map((t=>t.id));s.length&&(u(!0),fetch(`${a.api}/send_message?post`,{method:"post",body:JSON.stringify({member_ids:s,user_id:e.settings.user_id,subject:N.subject,message:N.message,token:o("vibebp").getToken()})}).then((t=>t.ok?t.json():{status:0,message:window.wplms_course_data.translations.error_loading_data})).then((t=>{if(t.message){let e=document.querySelector("#dash_contact_form_submit");e.textContent=t.message,setTimeout((function(){e.textContent=a.translations.send_message}),2e3),i("vibebp").addNotification({text:a.translations.message_sent})}u(!1)})).catch((t=>{console.error("Uh oh, an error!",t),i("vibebp").addNotification({text:window.wplms_course_data.translations.error_loading_data}),u(!1)})))}},a.translations.send_message):""))};document.addEventListener("wplms_dash_contact_users",(s=>{document.querySelector(".wplms_dash_contact_users")&&e(t(r,{settings:s.detail.widget.options}),document.querySelector(".wplms_dash_contact_users"))}))})();