let s=!1;function r(a,t){t?(a.classList.add("notifal-loading"),a.disabled=!0):(a.classList.remove("notifal-loading"),a.disabled=!1)}function c(a){window.NotifalUtils&&typeof window.NotifalUtils.showMessage=="function"&&window.NotifalUtils.showMessage(a,"error")}async function g(a){const t=window.NotifalCampaignToggleAjax||{},n=t.ajax_actions&&t.ajax_actions.toggle_campaign_status?t.ajax_actions.toggle_campaign_status:"notifal_toggle_campaign_status",e=t.nonce&&t.nonce.toggle_campaign_status?t.nonce.toggle_campaign_status:"",o=new FormData;o.append("action",n),o.append("nonce",e),o.append("campaign_id",a);const i=await fetch(t.ajax_url||"",{method:"POST",body:o,credentials:"same-origin"});if(!i.ok)throw new Error("HTTP "+String(i.status));return i.json()}async function d(a){const t=a.dataset.campaignId;if(t&&!a.classList.contains("notifal-loading"))try{r(a,!0);const n=await g(t);if(n&&n.success&&n.data){window.setTimeout(()=>{window.location.reload()},400);return}let e=window.NotifalCampaignStrings&&window.NotifalCampaignStrings.unexpected_error||"An unexpected error occurred. Please try again.";n&&n.data&&(typeof n.data=="string"?e=n.data:n.data.message&&(e=n.data.message)),c(e)}catch{c(window.NotifalCampaignStrings&&window.NotifalCampaignStrings.unexpected_error||"An unexpected error occurred. Please try again.")}finally{r(a,!1)}}function u(a){const t=a.target,n=t instanceof Element?t:t&&t.parentElement?t.parentElement:null;if(!n||typeof n.closest!="function")return;const e=n.closest(".notifal-campaign-status-toggle");!e||!(e instanceof HTMLButtonElement)||(a.preventDefault(),d(e))}function l(){s||(s=!0,document.addEventListener("click",u))}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",l):l();
