var config=pnScriptSetting.pn_config;if(firebase.apps.length||firebase.initializeApp(config),!messaging)var messaging=firebase.messaging();function pushnotification_load_messaging(){messaging.onTokenRefresh((()=>{messaging.getToken().then((e=>{console.log("Token refreshed."),push_notification_setTokenSentToServer(!1),sendTokenToServer(e),resetUI()})).catch((e=>{console.log("Unable to retrieve refreshed token ",e),showToken("Unable to retrieve refreshed token ",e)}))}));var e=setInterval((function(){"activated"==navigator.serviceWorker?.controller?.state&&(clearInterval(e),function(){function e(e){for(var t=null,n=e+"=",o=document.cookie.split(";"),i=0;i<o.length;i++){for(var r=o[i];" "==r.charAt(0);)r=r.substring(1,r.length);0==r.indexOf(n)&&(t=r.substring(n.length,r.length))}null===t&&(localStorage.getItem(e)&&localStorage.getItem(e+"_expiry")>Date.now()&&(t=localStorage.getItem(e)));return t}var t=pnScriptSetting.auto_segment_enabled||!1;if(t)try{var n=pnScriptSetting.auto_authors||[],o=pnScriptSetting.auto_categories||[],i=[],r=[],a=window.localStorage.getItem("pn_subscribed_authors");a&&(i=JSON.parse(a),Array.isArray(i)||(i=[]));var s=window.localStorage.getItem("pn_subscribed_cats");s&&(r=JSON.parse(s),Array.isArray(r)||(r=[])),i=i.map((function(e){return String(e)})),r=r.map((function(e){return String(e)})),n=n.map((function(e){return String(e)})),o=o.map((function(e){return String(e)}));var c=n.some((function(e){return-1===i.indexOf(e)})),g=o.some((function(e){return-1===r.indexOf(e)}));if(!c&&!g)return!1}catch(e){console&&console.log&&console.log("PN auto-segment localStorage parse error",e)}if(!t&&null!=e("pn_notification_block"))return!1;if(window.history.length<pnScriptSetting.popup_show_afternpageview)return!1;if("1"==pnScriptSetting.superpwa_apk_only){if("apk"!=sessionStorage.getItem("superpwa_mode"))return!1}if("1"==pnScriptSetting.pwaforwp_apk_only){if("apk"!=sessionStorage.getItem("pwaforwp_mode"))return!1}var p=1e3;pnScriptSetting.popup_show_afternseconds&&(p=1e3*parseInt(pnScriptSetting.popup_show_afternseconds)),setTimeout((function(){var e=document.getElementsByClassName("pn-wrapper");e.length>0&&(e[0].style.display="flex")}),p);document.getElementById("pn-activate-permission_link_nothanks")&&document.getElementById("pn-activate-permission_link_nothanks").addEventListener("click",(function(){var e=new Date;e.setDate(e.getDate()+parseInt(pnScriptSetting.notification_popup_show_again)),document.cookie="pn_notification_block=true;expires="+e.toUTCString()+";path="+pnScriptSetting.cookie_scope,localStorage.setItem("pn_notification_block",!0),localStorage.setItem("pn_notification_block_expiry",e.getTime());var t=document.getElementsByClassName("pn-wrapper");t&&(t[0].style.display="none")}));document.getElementById("pn-activate-permission_link")&&document.getElementById("pn-activate-permission_link").addEventListener("click",(function(){var e=document.getElementsByClassName("pn-wrapper");e&&(e[0].style.display="none"),messaging.requestPermission().then((function(){console.log("Notification permission granted.");var e=new Date;e.setDate(e.getDate()+parseInt(pnScriptSetting.notification_popup_show_again)),document.cookie="pn_notification_block=true;expires="+e.toUTCString()+";path="+pnScriptSetting.cookie_scope,document.cookie="notification_permission=granted;expires="+e.toUTCString()+";path="+pnScriptSetting.cookie_scope,localStorage.setItem("pn_notification_block",!0),localStorage.setItem("pn_notification_block_expiry",e.getTime()),localStorage.getItem("notification_permission","granted");try{if(pnScriptSetting.auto_segment_enabled){var t=pnScriptSetting.auto_authors||[],n=pnScriptSetting.auto_categories||[],o=[],i=[],r=window.localStorage.getItem("pn_subscribed_authors");r&&(o=JSON.parse(r),Array.isArray(o)||(o=[]));var a=window.localStorage.getItem("pn_subscribed_cats");a&&(i=JSON.parse(a),Array.isArray(i)||(i=[]));var s={};o.concat(t).forEach((function(e){s[String(e)]=!0}));var c={};i.concat(n).forEach((function(e){c[String(e)]=!0}));var g=Object.keys(s),p=Object.keys(c);window.localStorage.setItem("pn_subscribed_authors",JSON.stringify(g)),window.localStorage.setItem("pn_subscribed_cats",JSON.stringify(p))}}catch(e){console&&console.log&&console.log("PN auto-segment localStorage write error",e)}push_notification_isTokenSentToServer()?pnScriptSetting.auto_segment_enabled?messaging.getToken().then((function(e){e&&push_notification_saveToken(e)})).catch((function(e){console&&console.log&&console.log("PN getToken error",e)})):console.log("Token already saved"):push_notification_getRegToken()})).catch((function(e){if(Notification&&"denied"==Notification.permission){console.log("Notification permission denied.");var t=new Date;t.setDate(t.getDate()+pnScriptSetting.notification_popup_show_again),document.cookie="pn_notification_block=true;expires="+t+";path="+pnScriptSetting.cookie_scope,localStorage.setItem("pn_notification_block",!0),localStorage.setItem("pn_notification_block_expiry",t.getTime())}else console.log("Unable to get permission to notify.",e)}))}))}())}),2e3);messaging.onMessage((function(e){console.log("Message received (foreground). ",e),notificationTitle=e.data.title,notificationOptions={body:e.data.body,icon:e.data.icon,image:e.data.image,vibrate:[100,50,100],tag:"pn-"+(e.data.currentCampaign||Date.now()),data:{dateOfArrival:Date.now(),primarykey:e.data.currentCampaign,currentCampaign:e.data.currentCampaign,url:e.data.url||e.data.click_url}},"serviceWorker"in navigator?navigator.serviceWorker.ready.then((function(e){return e.showNotification(notificationTitle,notificationOptions)})).then((function(){console.log("PN Success (foreground): Notification shown via service worker")})).catch((function(e){console.error("PN Error (foreground): Failed to show notification via service worker",e)})):console.error("PN Error (foreground): Service Worker not supported")})),navigator.clearAppBadge?navigator.clearAppBadge():navigator.clearExperimentalAppBadge?navigator.clearExperimentalAppBadge():window.ExperimentalBadge&&window.ExperimentalBadge.clear()}function push_notification_getRegToken(e){messaging.getToken().then((function(e){e?(push_notification_saveToken(e),console.log(e),push_notification_setTokenSentToServer(!0)):(console.log("No Instance ID token available. Request permission to generate one."),push_notification_setTokenSentToServer(!1))})).catch((function(e){console.log("An error occurred while retrieving token. ",e),push_notification_setTokenSentToServer(!1)}))}function push_notification_setTokenSentToServer(e){window.localStorage.setItem("sentToServer",e?"1":"0")}function push_notification_isTokenSentToServer(){return"1"===window.localStorage.getItem("sentToServer")}function sendTokenToServer(e){push_notification_isTokenSentToServer()?console.log("Token already sent to server so won't send it again unless it changes"):(console.log("Sending token to server..."),push_notification_saveToken(e))}function pn_get_checket_cats(e,t){e.checked&&pn_cat_value.push(e.checked)}function push_notification_saveToken(e){var t=new XMLHttpRequest;t.onreadystatechange=function(){4==this.readyState&&200==this.status&&(200==this.responseText.status&&push_notification_setTokenSentToServer(!0),console.log(this.responseText))};var n=pnScriptSetting.auto_segment_enabled||!1,o=[],i=[];if(console.log("Auto-segment enabled:",n),console.log("Auto categories:",pnScriptSetting.auto_categories),console.log("Auto authors:",pnScriptSetting.auto_authors),n){try{var r=window.localStorage.getItem("pn_subscribed_authors"),a=window.localStorage.getItem("pn_subscribed_cats");if(r){var s=JSON.parse(r);i=Array.isArray(s)?s:[]}if(a){var c=JSON.parse(a);o=Array.isArray(c)?c:[]}0===o.length&&(pnScriptSetting.auto_categories||[]).length&&(o=pnScriptSetting.auto_categories||[]),0===i.length&&(pnScriptSetting.auto_authors||[]).length&&(i=pnScriptSetting.auto_authors||[])}catch(e){o=pnScriptSetting.auto_categories||[],i=pnScriptSetting.auto_authors||[]}console.log("Using auto data - categories:",o,"authors:",i)}else{const e=document.querySelectorAll("#pn-categories-checkboxes input:checked");for(var g=0;g<=e.length-1;g++)o.push(e[g].value);const t=document.querySelectorAll("#pn-author-checkboxes input:checked");for(g=0;g<=t.length-1;g++)i.push(t[g].value);console.log("Using manual selection - categories:",o,"authors:",i)}var p=[...i].join(","),l=[...o].join(",");console.log("Final category string:",l),console.log("Final author string:",p);var u=pushnotificationFCMGetOS(),d=pushnotificationFCMbrowserclientDetector(),f=window.location.href;t.open("POST",pnScriptSetting.ajax_url,!0),t.setRequestHeader("Content-type","application/x-www-form-urlencoded"),t.send("token_id="+e+"&category="+l+"&author="+p+"&user_agent="+d+"&os="+u+"&nonce="+pnScriptSetting.nonce+"&action=pn_register_subscribers&url="+f)}var pushnotificationFCMbrowserclientDetector=function(){var e=!!window.opr&&!!opr.addons||!!window.opera||navigator.userAgent.indexOf(" OPR/")>=0,t="undefined"!=typeof InstallTrigger,n=/constructor/i.test(window.HTMLElement)||(!window.safari||"undefined"!=typeof safari&&safari.pushNotification).toString(),o=!!document.documentMode,i=!o&&!!window.StyleMedia,r=!(!window.chrome||!window.chrome.webstore&&!window.chrome.runtime),a=(r||e)&&!!window.CSS;if(navigator.userAgent.match("CriOS"))return"Chrome ios";n=!!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);var s=/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream;return n&&s?"Safari ios":n?"Safari":t?"Firefox":r?"Chrome":e?"Opera":o?"IE":i?"Edge":a?"Blink":void 0},pushnotificationFCMGetOS=function(){var e=window.navigator.userAgent,t=window.navigator.platform,n=null;return-1!==["Macintosh","MacIntel","MacPPC","Mac68K"].indexOf(t)?n="Mac OS":-1!==["iPhone","iPad","iPod"].indexOf(t)?n="iOS":-1!==["Win32","Win64","Windows","WinCE"].indexOf(t)?n="Windows":/Android/.test(e)?n="Android":!n&&/Linux/.test(t)&&(n="Linux"),n};"0"==pnScriptSetting.pn_token_exists&&setTimeout((function(){messaging.getToken().then((function(e){e?(push_notification_saveToken(e),console.log(e),push_notification_setTokenSentToServer(!0)):(console.log("No Instance ID token available. Request permission to generate one."),push_notification_setTokenSentToServer(!1))})).catch((function(e){console.log("An error occurred while retrieving token. ",e),push_notification_setTokenSentToServer(!1)}))}),2e3),setTimeout((function(){if(document.querySelector(".pn-bell-button")){document.querySelector(".pn-bell-button").addEventListener("click",(()=>{document.cookie="pn_notification_block=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;",localStorage.removeItem("pn_notification_block"),location.reload()}))}}),2e3);