import{h}from"@stencil/core";import{sprintf,__,_x}from"@wordpress/i18n";import{speak}from"@wordpress/a11y";import{intervalString}from"../../../../functions/price";import{state as checkoutState}from"../../../../store/checkout/index";import{addCheckoutLineItem,removeCheckoutLineItem,trackOrderBump}from"../../../../store/checkout/mutations";export class ScOrderBump{constructor(){this.bump=void 0,this.showControl=void 0}lineItem(){var e,i,t;return null===(t=null===(i=null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.line_items)||void 0===i?void 0:i.data)||void 0===t?void 0:t.find((e=>{var i;return(null==e?void 0:e.bump)===(null===(i=this.bump)||void 0===i?void 0:i.id)}))}updateLineItem(){var e,i,t,o;const d=(null===(e=this.bump.price)||void 0===e?void 0:e.id)||(null===(i=this.bump)||void 0===i?void 0:i.price);if(this.lineItem())return removeCheckoutLineItem(null===(t=this.lineItem())||void 0===t?void 0:t.id),void speak(__("Order bump Removed.","surecart"));addCheckoutLineItem({bump:null===(o=this.bump)||void 0===o?void 0:o.id,price:d,quantity:1}),speak(__("Order bump applied.","surecart"))}componentDidLoad(){var e;trackOrderBump(null===(e=this.bump)||void 0===e?void 0:e.id)}newPrice(){var e,i,t,o,d,r;let a=null,n=(null===(i=null===(e=this.bump)||void 0===e?void 0:e.price)||void 0===i?void 0:i.amount)||0;if((null===(t=this.bump)||void 0===t?void 0:t.amount_off)&&(a=Math.max(0,n-(null===(o=this.bump)||void 0===o?void 0:o.amount_off))),null===(d=this.bump)||void 0===d?void 0:d.percent_off){const e=n*((null===(r=this.bump)||void 0===r?void 0:r.percent_off)/100);a=Math.max(0,n-e)}return a}renderInterval(){var e;const i=intervalString(null===(e=this.bump)||void 0===e?void 0:e.price,{labels:{interval:"/",period:__("for","surecart")}});return i.trim().length?h("span",{class:"bump__interval"},i):null}renderPrice(){var e,i,t,o,d,r,a,n,u;return h("div",{slot:"description",class:{bump__price:!0,"bump__price--has-discount":!!(null===(e=this.bump)||void 0===e?void 0:e.percent_off)||!!(null===(i=this.bump)||void 0===i?void 0:i.amount_off)},part:"price"},!(!(null===(t=this.bump)||void 0===t?void 0:t.percent_off)&&!(null===(o=this.bump)||void 0===o?void 0:o.amount_off))&&h("span",{"aria-label":
/** translators: %s: old price */
sprintf(__("Originally priced at %s.","surecart"),null===(d=this.bump)||void 0===d?void 0:d.subtotal_display_amount),class:"bump__original-price"},null===(r=this.bump)||void 0===r?void 0:r.subtotal_display_amount),h("span",null,h("span",{"aria-hidden":"true"},0===(null===(a=this.bump)||void 0===a?void 0:a.total_amount)&&__("Free","surecart"),(null===(n=this.bump)||void 0===n?void 0:n.total_amount)>0&&h("span",{class:"bump__new-price"},null===(u=this.bump)||void 0===u?void 0:u.total_display_amount),this.renderInterval())))}renderDiscount(){var e,i,t,o,d,r,a;return(null===(e=this.bump)||void 0===e?void 0:e.amount_off)?h("div",{class:"bump__tag","aria-label":
/** translators: %1$s: amount off, %2$s: currency */
sprintf(__("You save %1$s%2$s.","surecart"),null===(i=this.bump)||void 0===i?void 0:i.amount_off,(null===(t=this.bump)||void 0===t?void 0:t.price).currency)},h("span",{"aria-hidden":"true"},_x("Save","Save money","surecart")," ",null===(o=this.bump)||void 0===o?void 0:o.amount_off_display_amount)):(null===(d=this.bump)||void 0===d?void 0:d.percent_off)?h("div",{class:"bump__tag","aria-label":
/** translators: %s: amount percent off */
sprintf(__("You save %s%%.","surecart"),null===(r=this.bump)||void 0===r?void 0:r.percent_off)},h("span",{"aria-hidden":"true"},sprintf(
/** translators: %s: amount percent off */
_x("Save %s%%","Save money","surecart"),null===(a=this.bump)||void 0===a?void 0:a.percent_off))):void 0}render(){var e,i,t,o,d,r,a,n,u,l,s,c,p,v,m,b,_,f;const y=null===(i=null===(e=this.bump)||void 0===e?void 0:e.price)||void 0===i?void 0:i.product;return h("sc-choice",{key:"983d78c5de4e40b4d66f6e755e49000c3ba2e07f",value:null===(t=this.bump)||void 0===t?void 0:t.id,type:"checkbox",showControl:this.showControl,checked:!!this.lineItem(),onClick:e=>{e.preventDefault(),e.stopImmediatePropagation(),this.updateLineItem()},onKeyDown:e=>{"Enter"!==e.key&&" "!==e.key||(e.preventDefault(),e.stopImmediatePropagation(),this.updateLineItem())},exportparts:"base, control, checked-icon, title"},h("div",{key:"54a1819dbbfc23855d4a0920cd853973077fcb58",part:"base-content",class:"bump"},h("div",{key:"6261d9ede9267a35093f1271bcf48c98840cd605",class:"bump__text"},h("div",{key:"8eb1bd33b2592dc8358720b53ca3640bd03fd8b0",class:"bump__title","aria-label":sprintf(
/* translators: %s: order bump name */
__("Product: %s.","surecart"),(null===(d=null===(o=this.bump)||void 0===o?void 0:o.metadata)||void 0===d?void 0:d.cta)||(null===(r=this.bump)||void 0===r?void 0:r.name)||(null==y?void 0:y.name))},h("span",{key:"fd3214f1fc3f0e0bdd909d92d96339cc2a1b86a4","aria-hidden":"true"},(null===(n=null===(a=this.bump)||void 0===a?void 0:a.metadata)||void 0===n?void 0:n.cta)||(null===(u=this.bump)||void 0===u?void 0:u.name)||(null==y?void 0:y.name))),h("div",{key:"ef1e4657efc6dc4755641de2154d49e405f4d484",class:"bump__amount"},this.renderPrice(),this.renderDiscount()))),(null===(s=null===(l=this.bump)||void 0===l?void 0:l.metadata)||void 0===s?void 0:s.description)&&h("div",{key:"2e3a416ed1918562a4c1f49113cba05729b87ec3",slot:"footer",class:"bump__product--wrapper"},h("sc-divider",{key:"4d06a09e82fd1ecd41c01bdb048c5a76b0e33d69",style:{"--spacing":"var(--sc-spacing-medium)"}}),h("div",{key:"38c660a40700423b3f3a727f1db8f164a736daeb",class:"bump__product"},!!(null===(c=null==y?void 0:y.line_item_image)||void 0===c?void 0:c.src)&&h("img",{key:"df9c423d1eceadc3e61bc3530ae79c8bbb44a684",...null==y?void 0:y.line_item_image,class:"bump__image"}),h("div",{key:"0698bdcb6b3029cdccb97c5e7dc356cd60d3dab9",class:"bump__product-text"},!!(null===(v=null===(p=this.bump)||void 0===p?void 0:p.metadata)||void 0===v?void 0:v.cta)&&h("div",{key:"6f717ff6352c7fc1c48a4e29a934435bbb9406f1",class:"bump__product-title","aria-hidden":"true"},this.bump.name||(null==y?void 0:y.name)),!!(null===(b=null===(m=this.bump)||void 0===m?void 0:m.metadata)||void 0===b?void 0:b.description)&&h("div",{key:"4747ed4352a726279216fe07b174fd446b69b165",class:"bump__product-description","aria-label":sprintf(
/* translators: %s: Product description */
__("Product description: %s.","surecart"),null===(_=this.bump)||void 0===_?void 0:_.rendered_description)},h("span",{key:"f863722bbe36276c26bc2da9f0381f8917f800f6","aria-hidden":"true",innerHTML:null===(f=this.bump)||void 0===f?void 0:f.rendered_description}))))))}static get is(){return"sc-order-bump"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-order-bump.scss"]}}static get styleUrls(){return{$:["sc-order-bump.css"]}}static get properties(){return{bump:{type:"unknown",mutable:!1,complexType:{original:"Bump",resolved:"Bump",references:{Bump:{location:"import",path:"../../../../types",id:"src/types.ts::Bump"}}},required:!1,optional:!1,docs:{tags:[],text:"The bump"}},showControl:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:"Should we show the controls"},attribute:"show-control",reflect:!0}}}}