(()=>{"use strict";class e{constructor(e,t){this.ajaxUrl=e,this.nonce=t}async post(e,t={}){const r=new FormData;r.append("action",e),r.append("nonce",this.nonce),Object.entries(t).forEach(([e,t])=>{r.append(e,t)});const s=await fetch(this.ajaxUrl,{method:"POST",credentials:"same-origin",body:r}),n=await s.text();let a=n;try{a=n?JSON.parse(n):null}catch(e){a={_parseError:!0,raw:n,error:e?.message}}if(!s.ok){const e=a?.data?.error||a?.data?.message||a?.message||a?.raw||n||`HTTP Error ${s.status}`,t=new Error(String(e));throw t.status=s.status,t.body=a,t}if(a?._parseError){const e=String(a.raw||"").slice(0,300),t=new Error(`Invalid JSON response from server (status ${s.status}). Preview: ${e}`);throw t.status=s.status,t.body=a,t}if(!1===a?.success){const e=new Error(String(a?.data?.error||a?.data?.message||a?.message||"Request failed"));throw e.status=s.status,e.body=a,e}return a}}function t(e,t={},r=[]){const s=document.createElement(e);return Object.entries(t??{}).forEach(([e,t])=>{null!=t&&("className"===e?s.className=t:"dataset"===e?Object.entries(t).forEach(([e,t])=>s.dataset[e]=String(t)):e.startsWith("on")&&"function"==typeof t?s.addEventListener(e.substring(2).toLowerCase(),t):e in s?s[e]=t:s.setAttribute(e,String(t)))}),(Array.isArray(r)?r:[r]).flat(1/0).forEach(e=>{null!=e&&!1!==e&&("string"!=typeof e&&"number"!=typeof e?s.appendChild(e):s.appendChild(document.createTextNode(String(e))))}),s}class r{constructor(e="",t=null){this.containerSelector=e,this.apiService=t}renderNotice(e={}){return this.createNotice(this.containerSelector,e)}createNotice(e,r={}){const{type:s="info",message:n="",title:a="",dismissible:i=!0,timeoutMs:o=0}=r,c="string"==typeof e?document.querySelector(e):e;if(!c)return{destroy:()=>{},el:null};const d=[];if(a){const e=document.createTextNode(n),r=t("strong",{},[String(a)]),s=t("br",{},[]),i=t("p",{},[r,s,e]);d.push(i)}let l=null;i&&(l=t("button",{type:"button",className:"notice-dismiss",ariaLabel:"Dismiss"},[]),d.push(l));const u=t("div",{className:`notice notice-${s} ${i?"is-dismissible":""} tbk-admin-notice`,role:"alert"},d);c.prepend(u);let h=null;return o&&Number(o)>0&&(h=setTimeout(()=>this.destroy(h,u),Number(o))),i&&l&&l.addEventListener("click",()=>this.destroy(h,u)),{destroy:()=>this.destroy(h,u),el:u}}destroy(e,t){e&&clearTimeout(e),t&&"1"!==t?.dataset?.removing&&(t.dataset.removing="1",t.addEventListener("transitionend",this.handleTransitionEnd),t.classList.add("tbk-admin-notice--fade-out"),setTimeout(()=>{t.isConnected&&(t.removeEventListener("transitionend",this.handleTransitionEnd),t.remove())},350))}handleTransitionEnd(e){if(e.propertyName&&"opacity"!==e.propertyName)return;const t=e.currentTarget;t.removeEventListener("transitionend",this.handleTransitionEnd),t.remove()}async dismiss(e){try{await(this.apiService?.post?.("dismiss_notice",{notice_id:e}))}catch{}}initDismissListeners(e,t){const r=document.querySelector(this.containerSelector);r&&r.addEventListener("click",r=>{const s=r.target.closest(`.${e}`);if(!s)return;const n=s.closest(t);n?.id&&this.dismiss(n.id)})}}class s{constructor(e,t,r,s){this.apiService=e,this.buttonSelector=t,this.selectSelector=r,this.noticeManager=s}init(){const e=document.querySelector(this.buttonSelector);e?.addEventListener("click",e=>{e.preventDefault(),this.download()})}async download(){const e=document.querySelector(this.selectSelector),t=e?.value??null;if(t)try{const e=await this.checkPermission(t),r=e?.success;if(!r)return void this.noticeManager.renderNotice({type:"error",title:"Error en la descarga",message:e?.data?.error});globalThis.location.href=e.data.downloadUrl}catch(e){this.noticeManager.renderNotice({type:"error",title:"Error en la descarga",message:e.message||"Ocurrió un error al intentar descargar el archivo"})}else this.noticeManager.renderNotice({type:"error",title:"Error en la descarga",message:"Selecciona un archivo para descargar."})}async checkPermission(e){return await this.apiService.post("check_can_download_file",{file:e})}}(class{static ready(e){"function"==typeof e&&("loading"!==document.readyState?Promise.resolve().then(e):document.addEventListener("DOMContentLoaded",e,{once:!0}))}}).ready(()=>{if(!document.querySelector("#btnDownload"))return;const t=(()=>{const e=globalThis.ajax_object;return e?.ajax_url&&e?.nonce?e:null})();if(!t)return;const n=new e(t.ajax_url,t.nonce),a=new r("#logs-container");new s(n,"#btnDownload","#log_file",a).init()})})();