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.checkout=window.tec.tickets.commerce.square.checkout||{},((e,t,r)=>{"use strict";t.selectors={cardElement:"#tec-tc-gateway-square-card-element",cardErrors:"#tec-tc-gateway-square-errors",paymentMessage:"#tec-tc-gateway-square-payment-message",infoForm:".tribe-tickets__commerce-checkout-purchaser-info-wrapper",submitButton:"#tec-tc-gateway-square-checkout-button",hiddenElement:".tribe-common-a11y-hidden",form:".tribe-tickets__commerce-checkout-square-form"},t.square=null,t.card=null,t.checkoutContainer=null,t.handleErrorDisplay=(e,r=()=>{})=>{e.map((e=>t.showNotice({},"",e[1]))),r()},t.getRequestArgs=(e,r)=>{void 0===r&&(r={"X-WP-Nonce":t.data.nonce});const o={headers:r,hooks:{beforeRetry:[t.onBeforeRetry],beforeError:[t.onBeforeError]},timeout:3e4,throwHttpErrors:!1};return e&&(o.json=e),o},t.onBeforeRetry=async e=>(console.log(e),r.stop),t.onBeforeError=async e=>(console.log(e),r.stop),t.onPaymentError=r=>{tribe.tickets.debug.log("square","paymentError",r);const o=e(t.selectors.cardErrors);r?o.text(r.message).show():o.text("").hide()},t.getVerificationDetails=e=>{const r=e["purchaser-name"]||"",o=r.indexOf(" "),c=o>0?r.substring(0,o):r,a=o>0?r.substring(o+1):"";return{intent:"CHARGE",currencyCode:t.data.currencyCode,billingContact:{givenName:c,familyName:a,email:e["purchaser-email"],phone:e["purchaser-phone"]||null,countryCode:e["purchaser-country"]||null,addressLines:e["purchaser-address"]||null,state:e["purchaser-state"]||null,city:e["purchaser-city"]||null,postalCode:e["purchaser-postal-code"]||null},customerInitiated:!0,sellerKeyedIn:!1}},t.createPayment=async()=>{try{const e=await t.card.tokenize();if("OK"===e.status)await t.processPayment(e.token);else{let r="Card tokenization failed.";e.errors&&(r=e.errors.map((e=>e.message)).join(", ")),t.onPaymentError({message:r}),t.loader.hide()}}catch(e){t.onPaymentError({message:e.message||"An error occurred while processing the payment."}),t.loader.hide()}},t.processPayment=async e=>{const o={payment_source_id:e,purchaser:t.getPurchaserData()};try{const e=await r.post(t.data.orderEndpoint,t.getRequestArgs(o)).json();if(!e.success)throw new Error("Failed to create order.",{cause:e});e.redirect_url&&(window.location.href=e.redirect_url)}catch(e){t.onPaymentError({message:e.message||"An error occurred while processing the payment."}),t.loader.hide()}},t.initializeSquare=async()=>{try{if(!window.Square)return void console.error("Square SDK not loaded");const r=window.Square.payments(t.data.applicationId,t.data.locationId);t.card=await r.card(t.data.squareCardOptions),await t.card.attach(t.selectors.cardElement),e(t.selectors.form).on("submit",(e=>{e.preventDefault(),t.loader.show(),t.createPayment({})}))}catch(e){console.error("Failed to initialize Square",e),t.onPaymentError({message:"Failed to initialize payment form."})}},t.showNotice=(t,r,o,c=6e4)=>{if("function"!=typeof tribe.tickets.commerce.notice.show)return;let a={type:r||"error",message:o||"",delay:c};a=e.extend(a,t),tribe.tickets.commerce.notice.show(a)},t.loader={show:()=>{tribe.tickets.loader.show(e(t.selectors.form)),e(t.selectors.submitButton).prop("disabled",!0)},hide:()=>{tribe.tickets.loader.hide(e(t.selectors.form)),e(t.selectors.submitButton).prop("disabled",!1)}},t.ready=()=>{t.initializeSquare()},t.getPurchaserData=()=>tribe.tickets.commerce.getPurchaserData(e(t.selectors.infoForm)),e(t.ready)})(jQuery,window.tec.tickets.commerce.square.checkout,tribe.ky),window.tec=window.tec||{},window.tec.tickets=window.tec.tickets||{},window.tec.tickets.commerce=window.tec.tickets.commerce||{},window.tec.tickets.commerce.gateway=window.tec.tickets.commerce.gateway||{},window.tec.tickets.commerce.gateway.square=window.tec.tickets.commerce.gateway.square||{},window.tec.tickets.commerce.gateway.square.checkout={};