window.Rebound={VERSION:"1.0",URL_ROOT:"https://rebound.postmarkapp.com",URL_CHECK:"/check",COOKIE:"pm_rebound",isPolling:!1,bounceDetails:{},positions:["top","bottom","topLeft","topRight","bottomLeft","bottomRight"],config:{publicToken:null,email:null,appearance:"banner",position:"top",color:"red",title:"Please update your email",fallbackTitle:"Please update your email",description:"An email recently sent to [[email]] has bounced. Here’s why: [[reason]]",fallbackDescription:"An email recently sent to [[email]] has bounced.",actionUrl:null,actionLabel:"",repeat:null},init:function(){"object"==typeof window.ReboundSettings&&(this.userConfig=this.merge(this.config,window.ReboundSettings),this.check(),this.polling())},check:function(e){return e&&(this.userConfig=this.merge(this.config,e)),this.userConfig.publicToken?this.userConfig.email?(this.shouldTriggerRequest()&&this.request(),void this.polling()):console.warn("No email specified for Postmark rebound."):console.error("No Postmark Rebound public token")},polling:function(){this.validInterval()&&(this.isPolling=!0,setInterval(function(){window.Rebound&&window.Rebound.check()},this.userConfig.repeat))},validInterval:function(){return"number"==typeof this.userConfig.repeat&&this.userConfig.repeat>=5e3&&!this.isPolling},shouldTriggerRequest:function(){return!this.isAlertVisible&&this.hasBeenDismissed()},hasBeenDismissed:function(){var e=this.getCookie(window.Rebound.COOKIE)&&this.getCookie(window.Rebound.COOKIE)==encodeURIComponent(this.userConfig.email)||this.getCookie("pm-rebound-"+this.userConfig.email);return!e||"false"===e},request:function(){var e=new XMLHttpRequest,t=this.URL_ROOT+this.URL_CHECK,n={config:this.userConfig,version:this.VERSION};e.open("POST",t,!0),e.setRequestHeader("Content-Type","application/json; charset=utf-8"),e.onload=function(){var t=JSON.parse(e.responseText);4==e.readyState&&"200"==e.status?window.Rebound.handleSuccessResponse(t):console.error(t.error)},e.send(JSON.stringify(n))},handleSuccessResponse:function(e){e.inactive&&e.data&&(this.bounceDetails=e.data,e.styles&&"custom"!==this.userConfig.appearance&&(this.appendStyles(e.styles),this.showAlert()));var t=this.clone(e);delete t.styles,"function"==typeof this.userConfig.complete&&this.userConfig.complete(t)},appendStyles:function(e){var t=document.createElement("style");t.appendChild(document.createTextNode(e)),this.appendElement(t)},showAlert:function(){var e=document.createElement("div");e.classList.add("cleanslate"),e.classList.add("pm-rebound"),e.setAttribute("role","alert"),e.classList.add("pm-rebound--"+this.userConfig.appearance),e.classList.add("pm-rebound--color-"+this.userConfig.color),e.classList.add("pm-rebound--position-"+this.userConfig.position),e.appendChild(this.renderTitle()),e.appendChild(this.renderDescription()),this.userConfig.actionUrl&&e.appendChild(this.renderAction()),e.appendChild(this.renderClose()),this.appendElement(e),this.isAlertVisible=!0},renderTitle:function(){var e=document.createElement("h3"),t=this.renderStringTemplate(this.bounceDetails.expiredDetails?this.userConfig.fallbackTitle:this.userConfig.title);return e.classList.add("pm-rebound_title"),e.appendChild(document.createTextNode(t)),e},renderDescription:function(){var e=document.createElement("p"),t=this.renderStringTemplate(this.bounceDetails.expiredDetails?this.userConfig.fallbackDescription:this.userConfig.description);return e.classList.add("pm-rebound_desc"),e.insertAdjacentHTML("beforeend",Rebound.markdown(t)),e},renderStringTemplate:function(e){var t=e;for(var n in this.bounceDetails)this.bounceDetails.hasOwnProperty(n)&&(t=t.replace("[["+n+"]]",this.bounceDetails[n]));return t},markdown:function(e){return e="\n"+e+"\n",[{regex:/\[([^\[]+)\]\(([^\)]+)\)/g,replacement:"<a href='$2'>$1</a>"},{regex:/(\*\*)(.*?)\1/g,replacement:"<strong>$2</strong>"},{regex:/(__)(.*?)\1/g,replacement:"<em>$2</em>"}].forEach(function(t){e=e.replace(t.regex,t.replacement)}),e.trim()},renderAction:function(){var e=document.createElement("a");return e.classList.add("pm-rebound_action"),e.setAttribute("href",this.userConfig.actionUrl),e.appendChild(document.createTextNode(this.userConfig.actionLabel)),e.addEventListener("click",function(e){window.Rebound.closeAlert()}),e},renderClose:function(){var e=document.createElement("a");return e.classList.add("pm-rebound_close"),e.setAttribute("aria-label","Close alert"),e.appendChild(document.createTextNode("×")),e.addEventListener("click",function(e){window.Rebound.closeAlert()}),e},appendElement:function(e){if(this.testMode)document.querySelector(".js-preview").appendChild(e);else{var t=document.body.firstChild;t?t.parentNode.insertBefore(e,t):document.body.appendChild(e)}},closeAlert:function(){this.testMode||(document.querySelector(".pm-rebound").setAttribute("style","display: none !important;"),window.Rebound.setCookie(window.Rebound.COOKIE,encodeURIComponent(this.userConfig.email)),this.isAlertVisible=!1)},setCookie:function(e,t,n){var i="";if(n){var o=new Date;o.setTime(o.getTime()+24*n*60*60*1e3),i="; expires="+o.toUTCString()}document.cookie=e+"="+(t||"")+i+"; path=/"},getCookie:function(e){for(var t=e+"=",n=document.cookie.split(";"),i=0;i<n.length;i++){for(var o=n[i];" "==o.charAt(0);)o=o.substring(1,o.length);if(0==o.indexOf(t))return o.substring(t.length,o.length)}return null},clone:function(e){var t={};for(var n in e)"object"==typeof e[n]&&null!=e[n]?t[n]=this.clone(e[n]):t[n]=e[n];return t},merge:function(e,t){for(var n in t)try{t[n].constructor==Object?e[n]=MergeRecursive(e[n],t[n]):e[n]=t[n]}catch(i){e[n]=t[n]}return e}},window.Rebound.init();