var FireBoxClickTracker=function(){function t(){this.init()}var e=t.prototype;return e.init=function(){this.initEventListeners()},e.initEventListeners=function(){document.addEventListener("FireBoxFormOnSubmitClientValidated",this.handleFormSubmitClick.bind(this)),document.addEventListener("click",this.handleElementClick.bind(this)),document.addEventListener("FireBoxAfterOpen",this.handleCampaignOpen.bind(this)),document.addEventListener("FireBoxAfterClose",this.handleCampaignClose.bind(this))},e.handleFormSubmitClick=function(t){var e,i,n=t.detail.instance,r=t.detail.submitEvent;n&&r&&((i=(e=n.form.closest(".fb-inst"))?e.firebox:null)&&r.target&&this.trackClick(r.target,i,"form-submit"))},e.handleElementClick=function(t){var e,i=t.target.closest(".fb-inst");i&&i.firebox&&(e=i.firebox,t.target.matches(".firebox-block-button-element")||this.isTrackableElement(t.target)&&this.trackClick(t.target,e,"element-click"))},e.isTrackableElement=function(t){return t.matches('button, a, [role="button"], .fb-trackable, .firebox-block-button, .firebox-block-button-element')},e.handleCampaignOpen=function(t){var e=t.detail.instance;e&&this.bindClickTrackingForInstance(e)},e.handleCampaignClose=function(t){var e=t.detail.instance;e&&this.unbindClickTrackingForInstance(e)},e.trackClick=function(t,e,i){var n,r;void 0===i&&(i="generic"),!e.box_log_id||(n=this.getCleanButtonLabel(t))&&((r=this.getClicksFromCookie())[e.id]||(r[e.id]={box_log_id:e.box_log_id,clicks:[]}),r[e.id].clicks.find(function(t){return t.label===n})||(r[e.id].clicks.push({label:n,timestamp:Date.now()}),this.setClicksToCookie(r),e.emitEvent("afterClickTrack",{element:t,clickType:i,buttonLabel:n})))},e.getCleanButtonLabel=function(t){var e=t.tagName.toLowerCase();if(t.getAttribute("aria-label"))return t.getAttribute("aria-label").trim().substring(0,50);if(t.getAttribute("title"))return t.getAttribute("title").trim().substring(0,50);if("input"===e){var i=t.value||t.getAttribute("placeholder");return i?i.trim().substring(0,50):""}var n=this.getCleanText(t);if(n&&2<=n.length)return n.substring(0,50);if("button"===e||"submit"===t.type||"button"===t.type||"button"===t.getAttribute("role")||t.matches(".firebox-block-button-element, .firebox-block-button"))return"Submit Form";if("img"===e&&t.getAttribute("alt"))return t.getAttribute("alt").trim().substring(0,50);if("a"===e&&t.href){var r=t.href.split("/").pop()||t.href.split("#").pop();if(r&&r!==t.href&&3<r.length&&!r.includes("."))return r.substring(0,50)}return""},e.getCleanText=function(t){var e=t.cloneNode(!0);e.querySelectorAll("style, script").forEach(function(t){return t.remove()});var i=(e.textContent||"").trim();return!i||i.length<2?"":i},e.getClicksFromCookie=function(){var t=this.getCookie("firebox_clicks_tracker");if(!t)return{};try{return JSON.parse(t)}catch(t){return{}}},e.setClicksToCookie=function(e){Object.keys(e).forEach(function(t){e[t].clicks&&50<e[t].clicks.length&&(e[t].clicks=e[t].clicks.slice(-50))});var t=JSON.stringify(e);this.setCookie("firebox_clicks_tracker",t,1)},e.getCookie=function(t){for(var e=t+"=",i=document.cookie.split(";"),n=0;n<i.length;n++){for(var r=i[n];" "==r.charAt(0);)r=r.substring(1);if(0==r.indexOf(e))return r.substring(e.length,r.length)}return""},e.setCookie=function(t,e,i){var n=new Date;n.setTime(n.getTime()+24*i*60*60*1e3);var r="expires="+n.toUTCString();document.cookie=t+"="+e+";"+r+"; path=/; secure"},e.bindClickTrackingForInstance=function(r){var o=this,t=r.getContent();t&&(r.clickHandler&&t.removeEventListener("click",r.clickHandler),r.clickHandler=function(t){var e,i,n=t.target;n.closest("button.fb-close")||n.closest(".fb-backdrop")||("a"===(e=n.tagName.toLowerCase())||"button"===e||"submit"===n.type||"button"===n.type||"input"===e||"button"===n.getAttribute("role")||n.hasAttribute("onclick")||n.closest("[data-fbox]")||n.closest("[data-fbox-cmd]"))&&(i="generic","a"===e?i="link":"button"===e||"submit"===n.type||"button"===n.type?i="button":"input"===e?i="input":(n.closest("[data-fbox]")||n.closest("[data-fbox-cmd]"))&&(i="firebox-element"),o.trackClick(n,r,i))},t.addEventListener("click",r.clickHandler))},e.unbindClickTrackingForInstance=function(t){var e;t.clickHandler&&((e=t.getContent())&&e.removeEventListener("click",t.clickHandler),t.clickHandler=null)},t}();document.addEventListener("DOMContentLoaded",function(){window.FireBoxClickTracker=new FireBoxClickTracker});

