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 a=(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:a,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,a,r;let d=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)&&(d=Math.max(0,n-(null===(o=this.bump)||void 0===o?void 0:o.amount_off))),null===(a=this.bump)||void 0===a?void 0:a.percent_off){const e=n*((null===(r=this.bump)||void 0===r?void 0:r.percent_off)/100);d=Math.max(0,n-e)}return d}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,a,r,d,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===(a=this.bump)||void 0===a?void 0:a.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===(d=this.bump)||void 0===d?void 0:d.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,a;if(null===(e=this.bump)||void 0===e?void 0:e.amount_off)return 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));if(null===(a=this.bump)||void 0===a?void 0:a.percent_off){const e=`${this.bump.percent_off}%`;return h("div",{class:"bump__tag","aria-label":
/** translators: %s is the discount percentage (e.g. "10%"). */
sprintf(__("You save %s.","surecart"),e)},h("span",{"aria-hidden":"true"},sprintf(
/** translators: %s is the discount percentage (e.g. "10%"). */
_x("Save %s","Save money","surecart"),e)))}}render(){var e,i,t,o,a,r,d,n,u,l,s,c,p,m,v,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:"05bb127936345059e4a693ffb801f1d6f379b236",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:"631d30b5e3653656c7931271bf75a3cd202aa04f",part:"base-content",class:"bump"},h("div",{key:"10f22d4a4189c1a1a308c736c4b6e55d58cc0168",class:"bump__text"},h("div",{key:"ce19233d17e2760f9316c78c15f4f36a052bbd0c",class:"bump__title","aria-label":sprintf(
/* translators: %s: order bump name */
__("Product: %s.","surecart"),(null===(a=null===(o=this.bump)||void 0===o?void 0:o.metadata)||void 0===a?void 0:a.cta)||(null===(r=this.bump)||void 0===r?void 0:r.name)||(null==y?void 0:y.name))},h("span",{key:"e8b7c15c9b778ac317f7543d174af16f6eaa3851","aria-hidden":"true"},(null===(n=null===(d=this.bump)||void 0===d?void 0:d.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:"bfbec3b44de2eec1db920bb02ab4dc9ffe748629",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:"9ec1e3641218dfe944a6be2371c71e3012470ce6",slot:"footer",class:"bump__product--wrapper"},h("sc-divider",{key:"3c7dc55682f85184d55abbb968ba3da807b894a4",style:{"--spacing":"var(--sc-spacing-medium)"}}),h("div",{key:"369ec1658d2a6fca96c474c8a5b77a23f7a8e09d",class:"bump__product"},!!(null===(c=null==y?void 0:y.line_item_image)||void 0===c?void 0:c.src)&&h("img",{key:"a52a16131bf388ae077d1f08975ac016919ca577",...null==y?void 0:y.line_item_image,class:"bump__image"}),h("div",{key:"b25efd0119129c0b333f209aec14cae92419d225",class:"bump__product-text"},!!(null===(m=null===(p=this.bump)||void 0===p?void 0:p.metadata)||void 0===m?void 0:m.cta)&&h("div",{key:"0944412683e898b10ff10dff88d88a0663efdc95",class:"bump__product-title","aria-hidden":"true"},this.bump.name||(null==y?void 0:y.name)),!!(null===(b=null===(v=this.bump)||void 0===v?void 0:v.metadata)||void 0===b?void 0:b.description)&&h("div",{key:"05e8ce6ea11990b1a8be39c290a80dd499a46032",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:"30986f3ce04ac536b55660e021af758634886ca7","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}}}}