!function(){"use strict";const t={init(){this.bindEvents()},bindEvents(){document.addEventListener("click",(t=>{(t.target.classList.contains("wte-button-field")||t.target.closest(".wte-button-field"))&&this.handleButtonClick(t)}))},handleButtonClick(t){const e=t.target.classList.contains("wte-button-field")?t.target:t.target.closest(".wte-button-field");e.dataset.action&&(t.preventDefault(),e.disabled||this.processAjaxRequest(e))},processAjaxRequest(t){const e=t.dataset.action,n=t.dataset.nonce,a=t.dataset.params?JSON.parse(t.dataset.params):{};if(!e)return void this.showError(t,wteButtonField.i18n.error);const s=t.innerHTML;this.setButtonLoading(t,!0,s);const o=new FormData;o.append("action",e),o.append("_nonce",n),t.id&&o.append("button_id",t.id),t.name&&o.append("button_name",t.name);for(const e in t.dataset)if(t.dataset.hasOwnProperty(e)){if("action"===e||"nonce"===e)continue;o.append(e,t.dataset[e])}for(const t in a)a.hasOwnProperty(t)&&o.append(t,a[t]);this.triggerEvent(t,"wte:button:beforeAjax",{ajaxData:o}),fetch(wteButtonField.ajaxurl,{method:"POST",body:o,credentials:"same-origin"}).then((t=>t.json())).then((e=>{this.handleSuccess(t,e,s)})).catch((e=>{this.handleError(t,e,s)}))},handleSuccess(t,e,n){if(this.setButtonLoading(t,!1,n),e.success){if(this.triggerEvent(t,"wte:button:success",e.data),e.data&&e.data.message&&this.showNotice(t,e.data.message,"success"),e.data&&e.data.reload)return void setTimeout((()=>{window.location.reload()}),1e3);e.data&&e.data.redirect&&setTimeout((()=>{window.location.href=e.data.redirect}),1e3)}else{const n=e.data&&e.data.message?e.data.message:wteButtonField.i18n.error;this.showError(t,n),this.triggerEvent(t,"wte:button:error",e.data)}},handleError(t,e,n){this.setButtonLoading(t,!1,n);const a=e.message||wteButtonField.i18n.error;this.showError(t,a),this.triggerEvent(t,"wte:button:ajaxError",e)},setButtonLoading(t,e,n){e?(t.disabled=!0,t.classList.add("wte-button-loading"),t.dataset.originalText=n,t.innerHTML='<span class="wte-spinner"></span> '+wteButtonField.i18n.processing):(t.disabled=!1,t.classList.remove("wte-button-loading"),t.innerHTML=n)},showError(t,e){this.showNotice(t,e,"error")},showNotice(t,e,n="info"){t.parentElement.querySelectorAll(".wte-button-notice").forEach((t=>t.remove()));const a=document.createElement("div");a.className=`wte-button-notice wte-button-notice-${n}`,a.innerHTML=e,t.parentNode.insertBefore(a,t.nextSibling),setTimeout((()=>{a.style.transition="opacity 0.3s",a.style.opacity="0",setTimeout((()=>{a.remove()}),300)}),5e3),this.triggerEvent(t,"wte:button:notice",{message:e,type:n})},triggerEvent(t,e,n){const a=new CustomEvent(e,{detail:n,bubbles:!0,cancelable:!0});t.dispatchEvent(a)}};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",(()=>{t.init()})):t.init(),window.WTEButtonFieldHandler=t}();