class BrfPopups{popups=BRFPOPUPS.popups;apiUrl=BRFPOPUPS.apiurl;currentPage=BRFPOPUPS.currentPage;tl=gsap.timeline();isOpen=!1;constructor(){this.init()}init(){for(let e of this.popups)!e.template||!e.displayOn||!e.trigger|!e.active||(this.build(e),this.handleOpenFromBricks(e),this.handleClose(e))}build(e){switch(e.trigger){case"pageLoad":this.handleTriggerPageLoad(e);break;case"click":this.handleTriggerClick(e);break;case"scrollPosition":this.handleTriggerScrollPosition(e)}}handleTriggerPageLoad(e){setTimeout((()=>{this.fire(e)}),e.seconds?1e3*e.seconds:0)}handleTriggerClick(e){e.selector&&document.addEventListener("click",(t=>{t.target.matches(e.selector)&&(t.preventDefault(),this.fire(e))}),!1)}handleTriggerScrollPosition(e){e.scrollPosition&&document.addEventListener("scroll",(t=>{document.scrollingElement.scrollTop>=e.scrollPosition&&0==this.isOpen&&this.fire(e)}),!0)}async createNodes(e){let t=document.createElement("div"),o=document.createElement("div");if(t.classList.add("brf-popup"),t.classList.add("open"),o.classList.add("brf-popup-inner"),t.appendChild(o),e.overlayColor&&(t.style.backgroundColor=e.overlayColor),e.innerBackgroundColor&&(o.style.backgroundColor=e.innerBackgroundColor),e.addCloseButton){let e=document.createElement("div");e.classList.add("brf-popup-close"),e.classList.add("brf-close-btn"),e.innerHTML='<svg class="brf-popup-close" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">\x3c!--! Font Awesome Pro 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --\x3e<path d="M310.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L160 210.7 54.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L114.7 256 9.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L160 301.3 265.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L205.3 256 310.6 150.6z"/></svg>',t.appendChild(e)}e.isScrollable&&(t.style.alignItems="flex-start");let s=await this.getTemplateContent(e.template?e.template:"");switch(o.innerHTML=JSON.parse(s),document.body.appendChild(t),e.animation||(e.animation="fade-in-down"),e.animation){case"fade-in-down":default:this.tl.from(t,{opacity:0,duration:.5},0),this.tl.from(o,{opacity:0,translateY:-100},0),this.tl.play();break;case"fade-in-up":this.tl.from(t,{opacity:0,duration:.5},0),this.tl.from(o,{opacity:0,translateY:100},0),this.tl.play();break;case"fade-in-left":this.tl.from(t,{opacity:0,duration:.5},0),this.tl.from(o,{opacity:0,translateX:-100},0),this.tl.play();break;case"fade-in-right":this.tl.from(t,{opacity:0,duration:.5},0),this.tl.from(o,{opacity:0,translateX:100},0),this.tl.play();break;case"fade-in":this.tl.from(t,{opacity:0,duration:.5},0),this.tl.from(o,{opacity:0},0),this.tl.play()}}async getTemplateContent(e){let t=await fetch(this.apiUrl+"get_shortcode_content",{method:"POST",body:JSON.stringify(e),headers:{"X-WP-Nonce":BRFPOPUPS.nonce}});if(200==t.status){return await t.json()}}handleOpenFromBricks(e){document.addEventListener("click",(t=>{if(t.target.closest(".brf-popup-open")){let o=t.target.closest(".brf-popup-open");o.dataset.popup&&o.dataset.popup==e.id&&this.fire(e)}}),!1)}handleClose(e){document.addEventListener("click",(t=>{e.closeOnOverlayClick&&t.target.matches(".brf-popup")&&this.close(e);t.target.closest(".brf-popup-close")&&this.close(e)}),!1)}async allowed(e){switch(e.displayOn){case"entire_website":default:return!0;case"specific_page":if("object"==typeof e.page){if(e.page.length>0&&e.page.toString().includes(this.currentPage.toString()))return!0}else if(e.page&&this.currentPage==e.page)return!0;return!1}}async saveInStorage(e){let t="brf_popup_"+e.id,o=localStorage.getItem(t);return o=parseInt(o),o?o<e.executions&&(localStorage.setItem(t,o+1),!0):(localStorage.setItem(t,1),!0)}async fire(e){if(await this.allowed(e)){if(this.isOpen=!0,"click"!=e.trigger&&e.executions>=0){if(0==await this.saveInStorage(e))return}if(await this.createNodes(e),document.querySelector(".brf-popup .brxe-brf-pro-forms")&&bricksIsFrontend&&"undefined"!=typeof BrfProForms){document.querySelectorAll(".brxe-brf-pro-forms").forEach((e=>{new BrfProForms(e)}))}document.body.classList.add("brf-lock")}}async close(e){document.body.classList.remove("brf-lock"),await this.tl.reverse(),document.querySelector(".brf-popup").remove(),this.isOpen=!1}}document.addEventListener("DOMContentLoaded",(function(){bricksIsFrontend&&new BrfPopups}));