(()=>{"use strict";class t{constructor(t,e){this.ajaxUrl=t,this.nonce=e}async post(t,e={}){const s=new FormData;s.append("action",t),s.append("nonce",this.nonce),Object.entries(e).forEach(([t,e])=>{s.append(t,e)});const n=await fetch(this.ajaxUrl,{method:"POST",credentials:"same-origin",body:s}),a=await n.text();let r=a;try{r=a?JSON.parse(a):null}catch(t){r={_parseError:!0,raw:a,error:t?.message}}if(!n.ok){const t=r?.data?.error||r?.data?.message||r?.message||r?.raw||a||`HTTP Error ${n.status}`,e=new Error(String(t));throw e.status=n.status,e.body=r,e}if(r?._parseError){const t=String(r.raw||"").slice(0,300),e=new Error(`Invalid JSON response from server (status ${n.status}). Preview: ${t}`);throw e.status=n.status,e.body=r,e}if(!1===r?.success){const t=new Error(String(r?.data?.error||r?.data?.message||r?.message||"Request failed"));throw t.status=n.status,t.body=r,t}return r}}function e(t,e=""){const s="string"==typeof t?document.querySelector(t):t;s&&(s.textContent=e)}class s{constructor(t,e,s,n={}){this.apiService=t,this.buttonBusyManager=e,this.dom=s,this.config=n}init(){this.dom.checkButton?.addEventListener?.("click",t=>{t.preventDefault(),this.checkConnection()})}async checkConnection(){const t=function(t,e={},s=[]){const n=document.createElement(t);return Object.entries(e??{}).forEach(([t,e])=>{null!=e&&("className"===t?n.className=e:"dataset"===t?Object.entries(e).forEach(([t,e])=>n.dataset[t]=String(e)):t.startsWith("on")&&"function"==typeof e?n.addEventListener(t.substring(2).toLowerCase(),e):t in n?n[t]=e:n.setAttribute(t,String(e)))}),(Array.isArray(s)?s:[s]).flat(1/0).forEach(t=>{null!=t&&!1!==t&&("string"!=typeof t&&"number"!=typeof t?n.appendChild(t):n.appendChild(document.createTextNode(String(t))))}),n}("i",{className:"fa fa-spinner fa-spin"}),e=document.createTextNode("Verificando... "),s=this.buttonBusyManager.setBusy(this.dom.checkButton,[e,t]);this.setLoadingUIState();try{const t=await this.apiService.post(this.config.actionName,{product:this.config.productKey});this.applyResponseState(t)}catch(t){this.handleError(t)}finally{s()}}setLoadingUIState(){this.setContainerState("is-loading"),this.applyLoadingState()}applyResponseState(t){this.applyResultState(t),this.setContainerState("is-ready")}applyLoadingState(){this.dom.responseBadge&&(this.dom.responseBadge.textContent="Verificando",this.dom.responseBadge.classList.remove("label-success","label-danger")),e(this.dom.responseEnvironment,"Detectando...")}applyResultState(t){const s=t?.meta??{},n="OK"===t?.status?.string,a=n?"Conexión OK":"Conexión con error";e(this.dom.responseEnvironment,s?.environmentLabel??"No disponible"),this.dom.responseBadge&&(this.dom.responseBadge.textContent=a,this.dom.responseBadge.classList.remove("label-success","label-danger"),this.dom.responseBadge.classList.add(n?"label-success":"label-danger"))}handleError(){this.applyResultState({status:{string:"Error"},meta:{environmentLabel:"No disponible"}}),this.setContainerState("is-ready")}setContainerState(t){this.dom.resultContainer&&(this.dom.resultContainer.classList.remove("is-loading","is-ready"),this.dom.resultContainer.classList.add(t))}}class n{setBusy(t,e=[]){if(!t||t.classList.contains("tbk-is-busy"))return()=>{};const s=[...t.childNodes];t.classList.add("tbk-is-busy"),t.dataset.sending="true";const n=Array.isArray(e)?e:[e];return t.replaceChildren(...n),()=>{t.classList.remove("tbk-is-busy"),t.dataset.sending="false",t.replaceChildren(...s)}}}(class{static ready(t){"function"==typeof t&&("loading"!==document.readyState?Promise.resolve().then(t):document.addEventListener("DOMContentLoaded",t,{once:!0}))}}).ready(()=>{const e=document.querySelectorAll("[data-connection-test]");if(!e.length)return;const a=(()=>{const t=globalThis.ajax_object;return t?.ajax_url&&t?.nonce?t:null})();if(!a)return;const r=new t(a.ajax_url,a.nonce);e.forEach(t=>{const e=t.querySelector('[data-role="check-button"]'),a=t.querySelector('[data-role="result-container"]'),o=t.querySelector('[data-role="status-badge"]'),i=t.querySelector('[data-role="environment-value"]');e&&a&&o&&i&&new s(r,new n,{card:t,checkButton:e,resultContainer:a,responseBadge:o,responseEnvironment:i},{actionName:t.dataset.action||"check_connection",productKey:t.dataset.productKey||""}).init()})})})();