window.tec=window.tec||{},window.tec.tickets=window.tec.tickets||{},window.tec.tickets.commerce=window.tec.tickets.commerce||{},window.tec.tickets.commerce.square=window.tec.tickets.commerce.square||{},window.tec.tickets.commerce.square.webhooks=window.tec.tickets.commerce.square.webhooks||{},((e,t,o)=>{"use strict";const{__}=wp.i18n,s={copied:__("Copied!","event-tickets"),errorRegisteringWebhook:__("Failed to register webhook. Please try again.","event-tickets"),errorGeneric:__("An error occurred. Please try again.","event-tickets")},i="#tec-tickets__admin-settings-square-webhook-register",c="#square-test-mode",r=".square-live-field",n=".square-sandbox-field",a=()=>{e(c).is(":checked")?(e(r).closest(".tribe-field").hide(),e(n).closest(".tribe-field").show()):(e(r).closest(".tribe-field").show(),e(n).closest(".tribe-field").hide())},d=()=>{e(".tec-tickets-commerce-square-copy-button").each((function(){const t=e(this);t.data("clipboard-target")&&new window.tribe.clipboard(t),t.on("click",(function(){const t=e(this),o=t.text();t.text(s.copied),setTimeout((()=>{t.text(o)}),2e3)}))}))};o.registerWebhook=t=>{t.preventDefault();const o=e(t.currentTarget),i=o.siblings(".spinner"),{nonce:c}=o.data();o.prop("disabled",!0),i.addClass("is-active"),e.ajax({url:ajaxurl,type:"POST",data:{action:"tec_tickets_commerce_square_register_webhook",nonce:c},success:e=>{if(e.success)location.reload();else{const t=e.data?.message||s.errorRegisteringWebhook;alert(t),o.prop("disabled",!1),i.removeClass("is-active")}},error:()=>{alert(s.errorGeneric),o.prop("disabled",!1),i.removeClass("is-active")}})},o.registerWebhookFromNotice=t=>{t.preventDefault();const o=e(t.currentTarget),c=o.closest(".notice");c.find(".spinner").length||o.after('<span class="spinner is-active" style="float: none; margin-top: 0;"></span>'),o.css("pointer-events","none").css("opacity","0.5");let r=o.data("nonce");if(!r){const t=e(".tec-tickets__admin-settings-square-webhook-nonce");t.length&&(r=t.data("nonce"))}if(!r){const t=e(i);t.length&&(r=t.data("nonce"))}if(!r)return console.error("No webhook registration nonce found"),alert(s.errorGeneric),o.css("pointer-events","").css("opacity",""),void c.find(".spinner").remove();e.ajax({url:ajaxurl,type:"POST",data:{action:"tec_tickets_commerce_square_register_webhook",nonce:r},success:e=>{if(e.success)location.reload();else{const t=e.data?.message||s.errorRegisteringWebhook;alert(t),o.css("pointer-events","").css("opacity",""),c.find(".spinner").remove()}},error:()=>{alert(s.errorGeneric),o.css("pointer-events","").css("opacity",""),c.find(".spinner").remove()}})},o.handleFixWebhookClick=t=>{t.preventDefault();const o=e(t.currentTarget);o.siblings(".spinner").length||o.after('<span class="spinner is-active" style="float: none; margin-top: 0;"></span>'),o.prop("disabled",!0);const s=o.data("nonce");s?e.ajax({url:ajaxurl,type:"POST",data:{action:"tec_tickets_commerce_square_register_webhook",nonce:s},success:e=>{e.success?(o.closest(".notice").removeClass("notice-error").addClass("notice-success").find("p:first").html("<strong>"+__("Square webhook registered successfully!","event-tickets")+"</strong>"),setTimeout((()=>{location.reload()}),1500)):window.location=o.attr("href")},error:()=>{window.location=o.attr("href")}}):window.location=o.attr("href")},e((()=>{d(),e(".tec-tickets-commerce-square-test-webhook-button").on("click",o.testWebhook),e(c).on("change",a),e(i).on("click",o.registerWebhook),e(t).on("click",".tec-tickets__admin-settings-square-webhook-register-trigger",o.registerWebhookFromNotice),e(t).on("click",'.notice .button-primary[href*="admin.php?page=tec-tickets-settings&tab=payments&section=square"]',o.handleFixWebhookClick),a()}))})(jQuery,document,window.tec.tickets.commerce.square.webhooks),window.tec=window.tec||{},window.tec.tickets=window.tec.tickets||{},window.tec.tickets.admin=window.tec.tickets.admin||{},window.tec.tickets.admin.gateway=window.tec.tickets.admin.gateway||{},window.tec.tickets.admin.gateway.square=window.tec.tickets.admin.gateway.square||{},window.tec.tickets.admin.gateway.square.webhooks={};