import*as e from"@wordpress/interactivity";var t,o,n={d:function(e,t){for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}},r=(t={getContext:function(){return e.getContext},getElement:function(){return e.getElement},store:function(){return e.store}},o={},n.d(o,t),o);const{__,sprintf:i,_n}=wp.i18n,c="surecart-local-storage";let s=null;const u=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"live",t=arguments.length>1?arguments[1]:void 0;const o=localStorage.getItem(c),n={live:{},test:{}},r=(JSON.parse(o)||n)[e];return r?.[t]||n},a=e=>"keydown"===e.type&&"Enter"!==e.key&&" "!==e.key,l=e=>{const t=document.querySelector?.(e)||null;t&&setTimeout((()=>{t.focus()}),0)},{state:d,actions:m}=(0,r.store)("surecart/checkout",{state:{loading:!1,error:null,promotionCode:"",checkout:{},oldCheckout:{},get itemsCount(){return d.checkout?.line_items_count||0},get hasItems(){return d.itemsCount>0},get discountIsRedeemable(){return"redeemable"===d?.checkout?.discount?.redeemable_status},get lineItemHasScratchAmount(){const{line_item:e}=(0,r.getContext)();return!(e?.ad_hoc_amount||!e?.scratch_display_amount)&&e.scratch_amount!==e.subtotal_amount},get lineItemNote(){const{line_item:e}=(0,r.getContext)();return e?.display_note||""},get isDiscountApplied(){return!!d?.checkout?.discount?.promotion?.code},get hasDiscountAmount(){return!!d?.checkout?.discount_amount},get checkoutLineItems(){return(d?.checkout?.line_items?.data||[]).sort(((e,t)=>{const o=e?.price?.current_swap||e?.swap?1:0;return(t?.price?.current_swap||t?.swap?1:0)-o}))},get lineItemFees(){const{line_item:e}=(0,r.getContext)();return e?.fees?.data||[]},get isEditable(){const{line_item:e}=(0,r.getContext)();return!e?.price?.ad_hoc&&!e?.bump_amount},get hasSubscription(){return(d.checkout?.line_items?.data||[]).some((e=>"month"===e?.price?.recurring_interval&&!!e?.price?.recurring_interval&&!e?.price?.recurring_period_count))},get hasRecurring(){return d?.checkout?.line_items?.data?.some((e=>e?.price?.recurring_interval))},get swap(){const{line_item:e}=(0,r.getContext)();return e?.swap||e?.price?.current_swap},get swapDisplayAmount(){return d?.swap?.swap_price?.display_amount},get swapIntervalText(){return d?.swap?.swap_price?.short_interval_text},get swapIntervalCountText(){return d?.swap?.swap_price?.short_interval_count_text},get errorTitle(){return d.error?.title||d.error||""},get errorMessage(){return d.error?.message||""},get lineItemPermalink(){const{line_item:e}=(0,r.getContext)(),t=e?.price?.product;return t?.is_published?t?.permalink:null},get additionalErrors(){return(d?.error?.additional_errors||[]).map((e=>e.message))},get showCartMenuIcon(){const{cartMenuAlwaysShown:e}=(0,r.getContext)();return d.itemsCount>0||e},get itemsCountAriaLabel(){const e=d.itemsCount;return i(_n(/* translators: %d: number of items in the cart */
"Total of %d item in your cart","Total of %d items in your cart",e,"surecart"),e)},get lineItemAriaLabel(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return i(__("Cart item: %s. Quantity %d. Total price %s.","surecart"),e?.price?.product?.name,e?.quantity,e?.subtotal_display_amount)},get removeItemAriaLabel(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return i(__("Remove %s from your cart.","surecart"),e?.price?.product?.name)},get increaseQuantityAriaLabel(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return i(/* translators: %s: product name */
__("Increase quantity for %s.","surecart"),e?.price?.product?.name)},get decreaseQuantityAriaLabel(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return i(/* translators: %s: product name */
__("Decrease quantity for %s.","surecart"),e?.price?.product?.name)},get quantityInputAriaLabel(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return i(/* translators: %s: product name */
__("Quantity for %s.","surecart"),e?.price?.product?.name)},get lineItemVariant(){const{line_item:e}=(0,r.getContext)();return(e?.variant_options||[]).filter(Boolean).join(" / ")||null},get lineItemPriceName(){const{line_item:e}=(0,r.getContext)();return e.price.name??""},get showLineItemsCount(){const{line_item:e}=(0,r.getContext)();return e?.quantity>1},get isQuantityDisabled(){return!!d.loading},get isQuantityIncreaseDisabled(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return d.isQuantityDisabled||e?.max&&e?.quantity>=e?.max},get isQuantityDecreaseDisabled(){const{line_item:e}=(0,r.getContext)("surecart/checkout");return d.isQuantityDisabled||e?.quantity<=(e?.min||1)}},callbacks:{getState(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;return null===e?(0,r.getContext)():(0,r.getContext)()?.[e]||!1},init(){const{mode:e,formId:t}=(0,r.getContext)(),o=u(e,t);m.setCheckout(o,e,t)},syncTabs(e){if(e?.key!==c)return;const{mode:t,formId:o}=(0,r.getContext)(),n=u(t,o);m.setCheckout(n,t,o)},onChangeCheckout:function*(){const{checkout:e,oldCheckout:t}=d;if(JSON.stringify(e?.line_items?.data||[])===JSON.stringify(t?.line_items?.data||[]))return;const{processCheckoutEvents:o}=yield import("@surecart/checkout-events");o(e,t)}},actions:{fetch:function*(){const{mode:e,formId:t}=(0,r.getContext)()??{};if(!d.checkout?.id||!e||!t)return;const{fetchCheckout:o}=yield import("@surecart/checkout-service");try{const n=yield*o({id:d.checkout?.id});m.setCheckout(n,e,t)}catch(e){console.error(e),"checkout.not_found"===e?.code&&m.clearCheckouts()}},toggleDiscountInput(e){if(a(e))return!0;e.preventDefault();const t=(0,r.getContext)();t.discountInputOpen=!t.discountInputOpen;const{ref:o}=(0,r.getElement)(),n=o?.parentElement?.querySelector?.("input");n&&setTimeout((()=>n.focus()),0)},toggleSwap:function*(){const{line_item:e,mode:t,formId:o}=(0,r.getContext)(),{toggleSwap:n}=yield import("@surecart/checkout-service"),i=yield*n({id:e?.id,action:e?.swap?"unswap":"swap"});m.setCheckout(i,t,o)},setPromotionCode(e){d.promotionCode=e?.target?.value||""},maybeApplyDiscountOnKeyChange(e){if("Escape"!==e.key&&"Enter"!==e.key||(e.preventDefault(),e.stopPropagation()),"Escape"===e.key)return(0,r.getContext)().discountInputOpen=!1,void l("#sc-coupon-trigger");"Enter"===e.key&&m.applyDiscount(e),m.setPromotionCode(e)},applyDiscount:function*(e){if(e.preventDefault(),e.stopPropagation(),!d.promotionCode)return;const{mode:t,formId:o}=(0,r.getContext)(),{speak:n}=yield import("@wordpress/a11y");n(__("Applying promotion code.","surecart"),"assertive");const{handleCouponApply:c}=yield import("@surecart/checkout-service"),s=yield*c(d.promotionCode);s&&(n(i(/* translators: %s: promotion code */
__("Promotion code %s has been applied.","surecart"),d.promotionCode),"assertive"),d.error="",m.setCheckout(s,t,o),m.announceLatestCheckout(),l("#sc-coupon-remove-discount"))},removeDiscount:function*(){const e=(0,r.getContext)(),{mode:t,formId:o}=e,{speak:n}=yield import("@wordpress/a11y");n(__("Removing promotion code.","surecart"),"assertive");const{handleCouponApply:i}=yield import("@surecart/checkout-service"),c=yield*i(null);c&&(d.promotionCode="",e.discountInputOpen=!1,m.setCheckout(c,t,o),n(__("Promotion code has been removed.","surecart"),"assertive"),m.announceLatestCheckout(),l("#sc-coupon-trigger"))},closeCouponOnClickOutside:e=>{const t=(0,r.getContext)();e&&e.target.closest(".sc-coupon-form")||t.discountInputOpen&&(t.discountInputOpen=!1)},clearCheckouts(){let e=JSON.parse(localStorage.getItem(c));e={live:{},test:{}},localStorage.setItem(c,JSON.stringify(e)),d.checkout={}},setCheckout(e,t,o){let n=u(t,o);if(!n)return;d.oldCheckout=n;let r=JSON.parse(localStorage.getItem(c));r||(r={live:{},test:{}}),t&&e?.live_mode===("live"===t)&&(r={...r,[t]:{...r[t],[o]:e}},localStorage.setItem(c,JSON.stringify(r)),d.checkout=u(t,o))},onQuantityIncrease:function*(e){if(a(e))return!0;const{line_item:t}=(0,r.getContext)(),o=t?.quantity+1,{speak:n}=yield import("@wordpress/a11y");n(__("Updating quantity.","surecart"),"assertive"),yield m.updateLineItem({quantity:o}),n(i(/* translators: %d: quantity */
__("Quantity increased to %d.","surecart"),o),"assertive"),m.announceLatestCheckout()},onQuantityDecrease:function*(e){if(a(e))return!0;const{line_item:t}=(0,r.getContext)(),o=t?.quantity-1;if(o<1)return;const{speak:n}=yield import("@wordpress/a11y");n(__("Updating quantity.","surecart"),"assertive"),yield m.updateLineItem({quantity:o}),n(i(/* translators: %d: quantity */
__("Quantity decreased to %d.","surecart"),o),"assertive"),m.announceLatestCheckout()},onQuantityChange:function*(e){const t=parseInt(e.target.value||"");yield m.updateLineItem({quantity:t});const{speak:o}=yield import("@wordpress/a11y");o(i(/* translators: %d: quantity */
__("Quantity changed to %d.","surecart"),t),"assertive"),m.announceLatestCheckout()},updateLineItem:function*(e){d.loading=!0;const{line_item:t,mode:o,formId:n}=(0,r.getContext)(),{updateCheckoutLineItem:i}=yield import("@surecart/checkout-service"),c=yield*i({id:t?.id,data:e});m.setCheckout(c,o,n),d.loading=!1},removeLineItem:function*(e){if(a(e))return!0;e.preventDefault(),d.loading=!0;const{line_item:t,mode:o,formId:n}=(0,r.getContext)(),c=t?.price?.product?.name||__("item","surecart"),{speak:s}=yield import("@wordpress/a11y");s(i(__("Removing %s from your cart.","surecart"),c),"assertive");const{removeCheckoutLineItem:u}=yield import("@surecart/checkout-service"),l=yield*u(t?.id);m.setCheckout(l,o,n),d.loading=!1,s(i(__("Removed %s from your cart.","surecart"),c),"assertive"),m.announceLatestCheckout(),requestAnimationFrame((()=>{const e=document.querySelector(".wp-block-surecart-cart-line-item-remove, .sc-product-line-item__remove-button")||document.querySelector(".wp-block-surecart-cart-close-button");e?.focus()}))},updateCheckout(e){const{checkout:t,mode:o,formId:n}=e.detail;m.setCheckout(t,o,n)},announceLatestCheckout:function*(){const{speak:e}=yield import("@wordpress/a11y");clearTimeout(s),s=setTimeout((()=>{e(i(__("Checkout updated. The subtotal is %1$s.","surecart"),d?.checkout?.subtotal_display_amount),"polite")}),1e3)}}});addEventListener("scCheckoutUpdated",(e=>{document.querySelector("sc-checkout")||m.updateCheckout(e)}));