import{g as c,r as u}from"./api-DZ3cXm1m.js";document.addEventListener("DOMContentLoaded",()=>{document.querySelectorAll(".module-toggle-btn").forEach(e=>{e.addEventListener("click",s=>{s.preventDefault(),g(e)})}),document.querySelectorAll(".module-test-btn").forEach(e=>{e.addEventListener("click",s=>{s.preventDefault(),f(e)})})});async function g(e){var t,a;const s=e.closest("[data-module-id]"),o=s==null?void 0:s.dataset.toggleUrl;if(o){i(e,!0);try{const l=await d(o,{method:"POST"});if(!l.ok)throw new Error(`HTTP ${l.status}`);const r=await l.json(),n=s.querySelector(".module-status-badge");n&&(n.classList.toggle("bg-success",r.enabled),n.classList.toggle("bg-secondary",!r.enabled),n.textContent=r.enabled?((t=window.adminModules)==null?void 0:t.enabledLabel)??"Enabled":((a=window.adminModules)==null?void 0:a.disabledLabel)??"Disabled"),e.classList.toggle("btn-outline-warning",r.enabled),e.classList.toggle("btn-outline-success",!r.enabled),showToast==null||showToast("success",r.message??"OK")}catch(l){showToast==null||showToast("error",l.message||"Toggle failed")}finally{i(e,!1)}}}async function f(e){const s=e.closest("[data-module-id]"),o=s==null?void 0:s.dataset.testUrl;if(o){i(e,!0);try{const t=await d(o,{method:"POST"});if(!t.ok)throw new Error(`HTTP ${t.status}`);const a=await t.json();showToast==null||showToast(a.success?"success":"error",a.message)}catch(t){showToast==null||showToast("error",t.message||"Test failed")}finally{i(e,!1)}}}async function d(e,s={}){const o=a=>fetch(e,{...s,headers:{Accept:"application/json","X-Requested-With":"XMLHttpRequest",Authorization:`Bearer ${a}`,...s.headers??{}}});let t=await o(c());if(t.status===401){const a=await u();a&&(t=await o(a))}return t}function i(e,s){e.disabled=s,e.classList.toggle("disabled",s),s?(e.dataset.originalHtml=e.innerHTML,e.innerHTML='<span class="spinner-border spinner-border-sm"></span>'):e.dataset.originalHtml&&(e.innerHTML=e.dataset.originalHtml,delete e.dataset.originalHtml)}
