document.addEventListener("DOMContentLoaded",()=>{function t(){document.querySelectorAll(".notifal-status-toggle").forEach(t=>{"1"!==t.dataset.toggleBound&&(t.addEventListener("click",a),t.dataset.toggleBound="1")})}async function a(t){const a=t.currentTarget,o=a.dataset.notificationId,i=a.dataset.currentEnabled;if(!o||void 0===i)return;if(a.classList.contains("notifal-loading"))return;if(!("0"===i)||await async function(t){try{if(window.notifalProActive&&!0===window.notifalProActive)return!0;const a=document.querySelectorAll('.notifal-status-toggle[data-current-enabled="1"]'),e=Array.from(a).filter(a=>a.dataset.notificationId!==t).length;return 0===e}catch(a){return console.warn("Notifal: Frontend validation error, allowing (backend will validate):",a),!0}}(o))try{e(a,!0);const t=await async function(t){const a=new FormData;a.append("action","notifal_toggle_notification_status"),a.append("nonce",NotifalOnPageToggleAjax.nonce.toggle_status),a.append("notification_id",t);try{const t=await fetch(NotifalOnPageToggleAjax.ajax_url,{method:"POST",body:a,credentials:"same-origin"});if(!t.ok)throw new Error(`HTTP ${t.status}: ${t.statusText}`);return await t.json()}catch(e){throw e}}(o);if(t.success)return function(t,a){t.className=`notifal-status-badge notifal-status-toggle ${a.status_class}`,t.dataset.currentEnabled=a.is_enabled?"1":"0",t.dataset.currentStatus=a.new_status;const e=a.is_enabled?"disable":"enable";t.title=`Click to ${e} this notification`,t.setAttribute("aria-label",`Toggle notification status. Currently ${a.status_label}.`);const n=a.is_enabled?"check":"x-circle";t.innerHTML=`\n            <span class="notifal-icon notifal-icon-${n} size-16"></span>\n            ${a.status_label}\n        `}(a,t.data),void setTimeout(()=>{window.location.reload()},500);{let a=NotifalOnPageListStrings?.unexpected_error||"An unexpected error occurred. Please try again.";t.data&&("string"==typeof t.data?a=t.data:t.data.message&&(a=t.data.message)),n(a)}}catch(s){n(NotifalOnPageListStrings?.unexpected_error||"An unexpected error occurred. Please try again.")}finally{e(a,!1)}else n(NotifalOnPageListStrings?.free_version_limit||"You can only have one active notification in the free version. Upgrade to Notifal Pro to activate multiple notifications simultaneously.")}function e(t,a){a?(t.classList.add("notifal-loading"),t.disabled=!0):(t.classList.remove("notifal-loading"),t.disabled=!1)}function n(t){window.NotifalUtils&&"function"==typeof window.NotifalUtils.showMessage?window.NotifalUtils.showMessage(t,"error"):console.error("Notifal Error:",t)}t(),document.addEventListener("notifal:list:updated",t)});
