(()=>{"use strict";const e={init:function(){this.formElement=document.getElementById("feedbackForm"),this.errorSummary=document.getElementById("feedbackErrorSummary"),this.fields={yes:{comments:document.querySelector("#comments-yes")},no:{reason:document.querySelector("#reason-no"),comments:document.querySelector("#comments-no")},yesbut:{reason:document.querySelector("#reason-yesbut"),comments:document.querySelector("#comments-yesbut")}},this.formElement&&(this.attachEventHandlers(),this.addTracking())},addTracking:function(){[].slice.call(this.formElement.querySelectorAll(".ds_radio__input")).forEach((e=>{e.addEventListener("change",(()=>{const e=document.querySelector("[name=feedbacktype]:checked").value;this.updateDataLayer(e,"feedbackRadio")}))})),[].slice.call(this.formElement.querySelectorAll(".ds_select")).forEach((e=>{e.addEventListener("change",(()=>{const e=document.querySelector("[name=feedbacktype]:checked").value;this.updateDataLayer(e,"feedbackSelect")}))}))},attachEventHandlers:function(){this.formElement.addEventListener("submit",(e=>{e.preventDefault(),document.querySelector("[name=feedbacktype]:checked")&&this.submitFeedback()}))},submitFeedback:function(){if(!document.querySelector("[name=feedbacktype]:checked"))return;this.removeErrorMessages();const e=document.querySelector("[name=feedbacktype]:checked").value,t={slug:document.location.pathname,type:e,reason:this.getFeedbackReason(e),freetext:this.getFeedbackFreeText(e),category:document.querySelector("#page-category").value,errors:[],hippoContentItem:window.location.pathname,contentItem:document.getElementById("documentUuid").value},r=new URLSearchParams(window.location.search);r.has("q")&&(t.searchTerm=r.get("q")),r.has("cat")&&(t.searchCat=r.get("cat")),r.has("page")&&(t.searchPage=r.get("page"));const s=document.getElementById("feedbackErrorSummary"),a=s.querySelector(".ds_error-summary__content");if(a.innerHTML="",this.validateFeedback(t)){s.classList.add("fully-hidden"),this.updateDataLayer(t.type,"feedbackSubmit");var n=new XMLHttpRequest;n.open("POST","/service/feedback",!0),n.setRequestHeader("Content-Type","application/json; charset=utf-8");const e=this;n.onreadystatechange=function(){this.readyState===XMLHttpRequest.DONE&&201===this.status?(document.getElementById("feedbackThanks").classList.remove("fully-hidden"),e.formElement.classList.add("fully-hidden")):429===this.status?(e.errorSummary.querySelector(".ds_error-summary__content").innerHTML="<p>Sorry, too many requests have been submitted. Please try again later.</p>",e.errorSummary.classList.remove("fully-hidden"),e.errorSummary.scrollIntoView()):this.status>=400&&(e.errorSummary.querySelector(".ds_error-summary__content").innerHTML="<p>Sorry, we have a problem at our side. Please try again later.</p>",e.errorSummary.classList.remove("fully-hidden"),e.errorSummary.scrollIntoView())},n.send(JSON.stringify(t))}else{const e=document.createElement("ul");e.classList.add("ds_error-summary__list"),a.appendChild(e),t.errors.forEach((function(t){const r="SELECT"===t.field.nodeName?t.field.parentNode:t.field;r.classList.add("ds_input--error");const s=t.field.closest(".ds_question");s.classList.add("ds_question--error");let a=s.querySelector(".ds_question__error-message");a||(a=document.createElement("p"),a.classList.add("ds_question__error-message")),a.innerHTML=t.message,r.insertAdjacentElement("beforebegin",a);const n=document.createElement("li"),o=document.createElement("a");n.appendChild(o),e.appendChild(n),o.innerText=`${t.message}`,o.href=`#${t.field.id}`})),s.classList.remove("fully-hidden"),s.focus(),s.scrollIntoView()}},updateDataLayer:function(e,t){window.dataLayer=window.dataLayer||[],window.dataLayer.push({type:e,reason:this.getFeedbackReason(e),event:t})},getFeedbackReason:function(e){let t="";return"no"===e?t=this.fields.no.reason.value:"yesbut"===e&&(t=this.fields.yesbut.reason.value),t},getFeedbackFreeText:function(e){let t="";return t="no"===e?this.fields.no.comments.value:"yesbut"===e?this.fields.yesbut.comments.value:this.fields.yes.comments.value||"",t.substring(0,250)},removeErrorMessages:function(){this.errorSummary.classList.add("fully-hidden"),[].slice.call(this.formElement.querySelectorAll(".ds_input--error")).forEach((function(e){e.classList.remove("ds_input--error")})),[].slice.call(this.formElement.querySelectorAll(".ds_question--error")).forEach((function(e){e.classList.remove("ds_question--error")})),[].slice.call(this.formElement.querySelectorAll(".ds_question__error-message")).forEach((function(e){e.parentNode.removeChild(e)}))},validateFeedback:function(e){switch(e.type){case"no":""===e.freetext&&e.errors.push({field:this.fields.no.comments,message:"Please enter a comment"}),""===e.reason&&e.errors.push({field:this.fields.no.reason,message:"Please select a reason"});break;case"yesbut":e.freetext||e.errors.push({field:this.fields.yesbut.comments,message:"Please enter a comment"})}return 0===e.errors.length}},t=e,r={init:function(){t.init()}};window.format=r,window.format.init()})();