(()=>{"use strict";jQuery(document).ready(t=>{window.WC_Square_Gift_Card_Handler=class{constructor(i){this.applicationId=i.applicationId,this.locationId=i.locationId,this.giftCard=null,this.applyGiftCardNonce=i.applyGiftCardNonce,this.removeGiftCardNonce=i.removeGiftCardNonce,this.removeGiftCardText=i.removeGiftCardText,this.applyDiffGiftCard=i.applyDiffGiftCard,this.splitPaymentQuestion=i.splitPaymentQuestion,this.splitPaymentMessage=i.splitPaymentMessage,this.isValidPage=i.isValidPage,this.reviewOrderTable=t(".woocommerce-checkout-review-order-table"),this.payments=!!window.Square&&window.Square.payments(this.applicationId,this.locationId),this.handleSingProductPage(),t(document.body).on("updated_checkout",this.init.bind(this))}async init(i,e){if(this.payments){this.renderGiftCardHtml(e.fragments),this.initVariables(),this.block_ui(this.giftCardAppWrapper),t(document.body).on("update_checkout",()=>{this.hiddenFieldsEl&&this.hiddenFieldsEl.find('input[name="payment_method"]').remove(),this.block_ui(this.giftCardAppWrapper)});try{this.giftCard=await this.initializeGiftCard(this.payments)}catch(i){console.error("Initializing Gift Card failed",i)}this.giftCardAppWrapper.unblock(),this.giftCardBtnEl.on("click",async()=>{await this.applyGiftCardHandler(this.giftCard)}),this.splitPaymentEl.on("click",this.onSplitTotal.bind(this)),this.giftCard&&(this.giftCard.addEventListener("errorClassAdded",()=>{this.toggleGiftCardApplyButton(!0)}),this.giftCard.addEventListener("errorClassRemoved",()=>{this.toggleGiftCardApplyButton(!1)})),this.removeGiftCardEl.on("click",this.onRemoveGiftCard.bind(this))}}toggleDisabledAttribute(i=null,e=!1){i&&i.find("input, textarea").each((i,a)=>{t(a).attr("disabled",e)})}handleSingProductPage(){t('input[name="square-gift-card-buying-option"]').on("click",i=>{const e=t('[data-square-gift-card-activity="new"]'),a=t('[data-square-gift-card-activity="load"]');"new"===t(i.target).val()?(e.show(),a.hide(),this.toggleDisabledAttribute(e),this.toggleDisabledAttribute(a,!0)):(a.show(),e.hide(),this.toggleDisabledAttribute(a),this.toggleDisabledAttribute(e,!0))})}initVariables(){this.giftCardWrapperEl=t("#square-gift-card-fields-input"),this.giftCardAppWrapper=t("#square-gift-card-wrapper"),this.giftCardBtnEl=t("#square-gift-card-apply-btn"),this.removeGiftCardEl=t("#square-gift-card-remove"),this.splitPaymentEl=t("#square-split-payment")}toggleGiftCardApplyButton(t=!1){this.giftCardBtnEl.prop("disabled",t)}async initializeGiftCard(t=null){if(!t)return;const i=await t.giftCard();return await i.attach(this.giftCardWrapperEl[0]),i}renderGiftCardHtml(i){t("#square-gift-card-wrapper").remove(),t("#square-gift-card-split-details").remove(),t(".woocommerce-checkout-review-order-table").after(i[".woocommerce-square-gift-card-html"]),this.hiddenFieldsEl=t("#square-gift-card-hidden-fields"),this.hiddenFieldsEl&&i["has-balance"]?this.hiddenFieldsEl.append('<input name="payment_method" type="hidden" value="square_credit_card" />'):this.hiddenFieldsEl.find('input[name="payment_method"]').remove()}async applyGiftCardHandler(i){let e;this.block_ui(this.giftCardAppWrapper);try{e=await this.tokenize(i)}catch{i.setError("giftCardNumber"),this.giftCardAppWrapper.unblock()}e&&(await this.checkGiftCardBalance(e),t(document.body).trigger("update_checkout"))}async tokenize(t){const i=await t.tokenize();return"OK"===i.status&&i.token}async checkGiftCardBalance(t=null){const i=new FormData;i.append("token",t),i.append("action","wc_square_check_gift_card_balance"),i.append("security",this.applyGiftCardNonce);const e=await fetch(woocommerce_params.ajax_url,{method:"POST",body:i});if(200===e.status)return e.json()}async onRemoveGiftCard(i){i.preventDefault(),this.giftCard&&await this.giftCard.destroy()&&(this.giftCard=await this.initializeGiftCard(this.payments));const e=new FormData;e.append("action","wc_square_gift_card_remove"),e.append("security",this.removeGiftCardNonce);let a=await fetch(woocommerce_params.ajax_url,{method:"POST",body:e});200===a.status&&(a=await a.json(),a.success&&t(document.body).trigger("update_checkout"))}async onSplitTotal(i){i.preventDefault();const e=new FormData;e.append("action","wc_square_split_payments"),e.append("security",this.applyGiftCardNonce);let a=await fetch(woocommerce_params.ajax_url,{method:"POST",body:e});200===a.status&&(a=await a.json(),a.success&&t(document.body).trigger("update_checkout"))}block_ui(t=null){t&&t.block({message:null,overlayCSS:{background:"#fff",opacity:.6}})}}})})();