!function(){const t=document.querySelector(".learndash-virtual-instructor"),e={isEmptyBodyRequest:(t="")=>-1!==["GET","HEAD"].indexOf(t),request(t="GET",r={},o={},a=null){t=t.toUpperCase();const s=new FormData;Object.keys(o).forEach(t=>{s.append(t,o[t])});const n=new URL(learndashVirtualInstructor.ajaxurl),c=new URLSearchParams(s);n.search=c.toString();const i=e.isEmptyBodyRequest(t)?n.toString():learndashVirtualInstructor.ajaxurl;o=e.isEmptyBodyRequest(t)?null:s,fetch(i,{method:t,headers:r,body:o,credentials:"same-origin"}).then(t=>t.json()).then(a)},init(){e.id=t.dataset.id,e.course_id=t.dataset.course_id,e.request("POST",{},{action:learndashVirtualInstructor.actions.init,nonce:learndashVirtualInstructor.nonce.init,id:e.id,course_id:e.course_id},r=>{t.innerHTML=r.data.html,e.chatbox=t.querySelector(".ld-virtual-instructor-chatbox"),e.chatboxBody=t.querySelector(".ld-virtual-instructor-chatbox__body"),e.messageContainer=e.chatbox.querySelector(".ld-virtual-instructor-chatbox__messages-container")})},toggle(){e.chatbox.classList.toggle("ld-virtual-instructor-chatbox--open"),e.chatbox.classList.toggle("ld-virtual-instructor-chatbox--close"),e.chatbox.querySelector(".ld-virtual-instructor-chatbox__header-button").classList.toggle("ld-virtual-instructor-chatbox__header-button--open"),e.chatbox.querySelector(".ld-virtual-instructor-chatbox__header-button").classList.toggle("ld-virtual-instructor-chatbox__header-button--close")},toggleInput(){e.chatbox.querySelector(".ld-virtual-instructor-chatbox__form-input").toggleAttribute("disabled"),e.chatbox.querySelector(".ld-virtual-instructor-chatbox__form-input").focus(),e.chatbox.querySelector(".ld-virtual-instructor-chatbox__loader").classList.toggle("ld-virtual-instructor-chatbox__loader--visible"),e.chatbox.querySelector(".ld-virtual-instructor-chatbox__loader").classList.toggle("ld-virtual-instructor-chatbox__loader--hidden"),e.scrollToBottom()},send(t){e.request("POST",{},{action:learndashVirtualInstructor.actions.send,nonce:learndashVirtualInstructor.nonce.send,id:e.id,course_id:e.course_id,message:t},t=>{e.toggleInput(),e.addMessageHtml(t.data.html)})},scrollToBottom(){e.chatboxBody.scrollTo(0,e.chatboxBody.scrollHeight,{behavior:"smooth"})},addMessageHtml(t){e.messageContainer.insertAdjacentHTML("beforeend",t),e.scrollToBottom()},inputMessage:t=>{e.request("POST",{},{action:learndashVirtualInstructor.actions.input,nonce:learndashVirtualInstructor.nonce.input,id:e.id,course_id:e.course_id,message:t},r=>{e.addMessageHtml(r.data.html),e.toggleInput(),e.send(t)})}};document.addEventListener("DOMContentLoaded",()=>{e.init(),document.addEventListener("click",t=>{"ld-virtual-instructor-chatbox__header-button"===t.target.id&&e.toggle()}),document.addEventListener("submit",t=>{if("ld-virtual-instructor-chatbox__form"!==t.target.id)return;t.preventDefault();const r=t.target.querySelector(".ld-virtual-instructor-chatbox__form-input"),o=r.value;if(o.trim().length<1)return!1;r.value="",e.inputMessage(o)})})}();