(()=>{"use strict";const e=window.React,t=function({paymentMethodConfigurationId:t,eventRegistration:n}){const{onCheckoutSuccess:o,onCheckoutValidation:r}=n,[a,c]=(0,e.useState)(!0),i=`payment-method-${t}`;let s=window.IframeCheckoutHandler(t);const u=(0,e.useCallback)((()=>{s.create(i),c(!1)}),[t,i]);return(0,e.useEffect)((()=>{document.getElementById(i)&&u();const e=o((()=>new Promise((e=>{s.submit(),setTimeout((function(){e(!1)}),3e4)})))),t=r((()=>{let e=new Promise((e=>{s.setValidationCallback((t=>{void 0!==t.success?e(t.success):(console.error("Validation was not successful"),e(!1))}))}));return s.validate(),e}));return()=>{e(),t()}}),[u,i,o,r]),(0,e.createElement)("div",null,a&&(0,e.createElement)("div",null,"Loading payment method..."),(0,e.createElement)("div",{id:i}))},n=function({paymentMethodConfigurationId:t,eventRegistration:n}){const{onCheckoutSuccess:o}=n;(0,e.useEffect)((()=>{const e=o((()=>new Promise(((e,n)=>{window.LightboxCheckoutHandler.startPayment(t,(function(t,o){!t||t.error?n(t):e(t)}))})).catch((e=>{console.error(e)}))));return()=>{e()}}),[o,t])},o=function({paymentMethodConfigurationId:e,paymentPageUrl:t,eventRegistration:n}){const{onCheckoutSuccess:o}=n};function r({label:t,src:n,alt:o,width:r}){return(0,e.createElement)("div",null," ",t," ",(0,e.createElement)("img",{src:n,alt:o,width:r})," ")}const a=window.wc.wcBlocksRegistry,c=async(e={})=>{const{updateTransaction:t=!1,enqueuePortalScripts:n=!1}=e;try{const e=new URLSearchParams;e.append("action","weareplanet_get_payment_methods"),e.append("weareplanet_nonce",window.weareplanet_block_params?.weareplanet_nonce||""),t&&e.append("updateTransaction","1"),n&&e.append("enqueuePortalScripts","1");const o=await fetch(wp.apiFetch.nonceEndpoint,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:e});if(!o.ok)throw new Error(`HTTP error! Status: ${o.status}`);const r=await o.json();if(!r)throw new Error("No payment methods found");return r}catch(e){console.error("Error fetching payment methods:",e)}},i=async c=>{c.forEach((c=>{const i=(()=>{switch(c.integration_mode){case"iframe":return(0,e.createElement)(t,{paymentMethodConfigurationId:c.configuration_id});case"lightbox":return(0,e.createElement)(n,{paymentMethodConfigurationId:c.configuration_id});default:return(0,e.createElement)(o,{paymentMethodConfigurationId:c.configuration_id})}})(),s=function(e){const t=e.match(/src="([^"]+)"/),n=e.match(/alt="([^"]*)"/),o=e.match(/width="([^"]+)"/);return{src:t?t[1]:"",alt:n?n[1]:"",width:o?o[1]:""}}(c.icon);(0,a.registerPaymentMethod)({name:c.name,label:(0,e.createElement)(r,{label:c.label,...s}),content:i,edit:(0,e.createElement)("div",null,c.description),ariaLabel:c.ariaLabel,supports:{features:c.supports},canMakePayment:()=>Promise.resolve(c.isActive)})}))},s=async(e={})=>{try{const t=await c(e),n=(0,a.getPaymentMethods)();t.forEach((e=>{var t;const o={...null!==(t=n[e.name])&&void 0!==t&&t,canMakePayment:()=>e.isActive};(0,a.registerPaymentMethod)(o)}))}catch(e){console.error("Error handling batch response:",e)}};if(document.body.classList.contains("woocommerce-checkout")&&!document.body.classList.contains("woocommerce-order-received")){const e=document.getElementById("weareplanet-payment-methods")?.getAttribute("data-json");if(e)try{const t=JSON.parse(e);i(t)}catch(e){console.error("Error parsing JSON from HTML:",e)}else(async()=>{try{const e=await c({updateTransaction:!0,enqueuePortalScripts:!0});i(e)}catch(e){console.error("Error initializing payment methods:",e)}})();!function(){const e=window.fetch;window.fetch=async(...t)=>{const n=await e(...t),o=t[0];if("string"==typeof o&&o.includes("/wc/store/v1/batch")){const e=t[1]?.body?JSON.parse(t[1].body):null;e&&e.requests&&Array.isArray(e.requests)&&(e.requests.some((e=>"/wc/store/v1/cart/update-customer"===e.path))?function(e){for(const t of e){const e=(t.getAttribute("type")||"").toLowerCase();if("select"!==t.tagName.toLowerCase()){if("checkbox"!==e)if("radio"!==e){if(!t.value.toString().trim())return!1}else{const e=t.getAttribute("name");if(!document.querySelector(`input[type="radio"][name="${e}"]:checked`))return!1}}else if(!t.value)return!1}return!0}((document.querySelector("form.woocommerce-checkout")||document.querySelector("form.checkout")||document).querySelectorAll('[required], [aria-required="true"]'))&&s():"woocommerce-gift-cards"===e.requests?.[0]?.data?.namespace?s({updateTransaction:!0}):(e.requests.some((e=>"/wc/store/v1/cart/apply-coupon"===e.path))||e.requests.some((e=>"/wc/store/v1/cart/remove-coupon"===e.path)))&&s({enqueuePortalScripts:!0}))}return n}}()}})();