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