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.isModern=!0,this.bump=void 0,this.showControl=void 0,this.loading=!1}lineItem(){var i,t,e;return null===(e=null===(t=null===(i=null==checkoutState?void 0:checkoutState.checkout)||void 0===i?void 0:i.line_items)||void 0===t?void 0:t.data)||void 0===e?void 0:e.find((i=>{var t;return(null==i?void 0:i.bump)===(null===(t=this.bump)||void 0===t?void 0:t.id)}))}async updateLineItem(){var i,t,e;if(this.loading)return;const o=(null===(i=this.bump.price)||void 0===i?void 0:i.id)||(null===(t=this.bump)||void 0===t?void 0:t.price),r=this.lineItem();this.loading=!0;try{r?(await removeCheckoutLineItem(r.id),speak(__("Order bump Removed.","surecart"))):(await addCheckoutLineItem({bump:null===(e=this.bump)||void 0===e?void 0:e.id,price:o,quantity:1}),speak(__("Order bump applied.","surecart")))}catch(i){console.error("[sc-order-bump] updateLineItem failed:",i),speak(__("Something went wrong. Please try again.","surecart"))}finally{this.loading=!1}}componentWillLoad(){var i;this.isModern=!!(null===(i=window.scData)||void 0===i?void 0:i.modern_order_bump),this.el.setAttribute("data-design",this.isModern?"modern":"classic")}componentDidLoad(){var i;trackOrderBump(null===(i=this.bump)||void 0===i?void 0:i.id)}renderInterval(){var i;const t=intervalString(null===(i=this.bump)||void 0===i?void 0:i.price,{labels:{interval:"/",period:__("for","surecart")}});return t.trim().length?h("span",{class:"bump__interval"},t):null}renderDiscount(){var i,t,e,o,r;if(null===(i=this.bump)||void 0===i?void 0:i.amount_off)return h("div",{class:"bump__tag",part:"tag","aria-label":
/** translators: %1$s: amount off, %2$s: currency */
sprintf(__("You save %1$s%2$s.","surecart"),null===(t=this.bump)||void 0===t?void 0:t.amount_off,(null===(e=this.bump)||void 0===e?void 0:e.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===(r=this.bump)||void 0===r?void 0:r.percent_off){const i=`${this.bump.percent_off}%`;return h("div",{class:"bump__tag",part:"tag","aria-label":
/** translators: %s is the discount percentage (e.g. "10%"). */
sprintf(__("You save %s.","surecart"),i)},h("span",{"aria-hidden":"true"},sprintf(
/** translators: %s is the discount percentage (e.g. "10%"). */
_x("Save %s","Save money","surecart"),i)))}}renderModernPrice(){var i,t,e,o,r,n,a,s,d;return h("div",{class:{bump__price:!0,"bump__price--has-discount":!!(null===(i=this.bump)||void 0===i?void 0:i.percent_off)||!!(null===(t=this.bump)||void 0===t?void 0:t.amount_off)},part:"price"},!(!(null===(e=this.bump)||void 0===e?void 0:e.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===(r=this.bump)||void 0===r?void 0:r.subtotal_display_amount),class:"bump__original-price"},null===(n=this.bump)||void 0===n?void 0:n.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===(s=this.bump)||void 0===s?void 0:s.total_amount)>0&&h("span",{class:"bump__new-price"},null===(d=this.bump)||void 0===d?void 0:d.total_display_amount),this.renderInterval())))}renderClassicPrice(){var i,t,e,o,r,n,a,s,d;return h("div",{slot:"description",class:{bump__price:!0,"bump__price--has-discount":!!(null===(i=this.bump)||void 0===i?void 0:i.percent_off)||!!(null===(t=this.bump)||void 0===t?void 0:t.amount_off)},part:"price"},!(!(null===(e=this.bump)||void 0===e?void 0:e.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===(r=this.bump)||void 0===r?void 0:r.subtotal_display_amount),class:"bump__original-price"},null===(n=this.bump)||void 0===n?void 0:n.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===(s=this.bump)||void 0===s?void 0:s.total_amount)>0&&h("span",{class:"bump__new-price"},null===(d=this.bump)||void 0===d?void 0:d.total_display_amount),this.renderInterval())))}renderModern(){var i,t,e,o,r,n,a,s,d,l,u,p,c,m;const v=null===(t=null===(i=this.bump)||void 0===i?void 0:i.price)||void 0===t?void 0:t.product,_=this.lineItem();return h("sc-choice",{value:null===(e=this.bump)||void 0===e?void 0:e.id,type:"checkbox",showControl:!1,checked:!!_,exportparts:"base:choice__base, content:choice__content"},h("div",{part:"base-content",class:"bump",onClick:i=>i.stopPropagation(),onKeyDown:i=>{"Enter"!==i.key&&" "!==i.key||i.stopPropagation()}},!!(null===(o=null==v?void 0:v.line_item_image)||void 0===o?void 0:o.src)&&h("img",{...null==v?void 0:v.line_item_image,class:"bump__image",part:"image"}),h("div",{class:"bump__text",part:"text"},h("div",{class:"bump__title",part:"title","aria-label":sprintf(
/* translators: %s: order bump name */
__("Product: %s.","surecart"),(null===(r=this.bump)||void 0===r?void 0:r.name)||(null==v?void 0:v.name))},h("span",{"aria-hidden":"true"},(null===(n=this.bump)||void 0===n?void 0:n.name)||(null==v?void 0:v.name))),!!(null===(s=null===(a=this.bump)||void 0===a?void 0:a.metadata)||void 0===s?void 0:s.cta)&&h("div",{class:"bump__cta",part:"cta"},h("span",null,null===(l=null===(d=this.bump)||void 0===d?void 0:d.metadata)||void 0===l?void 0:l.cta)),h("div",{class:"bump__amount",part:"amount"},this.renderModernPrice(),this.renderDiscount()),!!(null===(p=null===(u=this.bump)||void 0===u?void 0:u.metadata)||void 0===p?void 0:p.description)&&h("div",{class:"bump__description",part:"description","aria-label":sprintf(
/* translators: %s: Product description */
__("Product description: %s.","surecart"),null===(c=this.bump)||void 0===c?void 0:c.rendered_description)},h("span",{"aria-hidden":"true",innerHTML:null===(m=this.bump)||void 0===m?void 0:m.rendered_description}))),h("button",{type:"button",class:{bump__button:!0,"bump__button--checked":!!_,"bump__button--loading":this.loading},part:"button",onClick:()=>this.updateLineItem(),"aria-label":__(_?"Remove from order":"Add to order","surecart"),"aria-pressed":!!_,"aria-busy":this.loading},this.loading?h("sc-spinner",null):h("sc-icon",{name:_?"check":"plus"}))))}renderClassic(){var i,t,e,o,r,n,a,s,d,l,u,p,c,m,v,_,b,f;const g=null===(t=null===(i=this.bump)||void 0===i?void 0:i.price)||void 0===t?void 0:t.product;return h("sc-choice",{value:null===(e=this.bump)||void 0===e?void 0:e.id,type:"checkbox",showControl:this.showControl,checked:!!this.lineItem(),disabled:this.loading,onClick:i=>{i.preventDefault(),i.stopImmediatePropagation(),this.updateLineItem()},onKeyDown:i=>{"Enter"!==i.key&&" "!==i.key||(i.preventDefault(),i.stopImmediatePropagation(),this.updateLineItem())},exportparts:"base, control, checked-icon, title"},h("div",{part:"base-content",class:"bump"},h("div",{class:"bump__text"},h("div",{class:"bump__title","aria-label":sprintf(
/* translators: %s: order bump name */
__("Product: %s.","surecart"),(null===(r=null===(o=this.bump)||void 0===o?void 0:o.metadata)||void 0===r?void 0:r.cta)||(null===(n=this.bump)||void 0===n?void 0:n.name)||(null==g?void 0:g.name))},h("span",{"aria-hidden":"true"},(null===(s=null===(a=this.bump)||void 0===a?void 0:a.metadata)||void 0===s?void 0:s.cta)||(null===(d=this.bump)||void 0===d?void 0:d.name)||(null==g?void 0:g.name))),h("div",{class:"bump__amount"},this.renderClassicPrice(),this.renderDiscount()))),(null===(u=null===(l=this.bump)||void 0===l?void 0:l.metadata)||void 0===u?void 0:u.description)&&h("div",{slot:"footer",class:"bump__product--wrapper"},h("sc-divider",{style:{"--spacing":"var(--sc-spacing-medium)"}}),h("div",{class:"bump__product"},!!(null===(p=null==g?void 0:g.line_item_image)||void 0===p?void 0:p.src)&&h("img",{...null==g?void 0:g.line_item_image,class:"bump__image"}),h("div",{class:"bump__product-text"},!!(null===(m=null===(c=this.bump)||void 0===c?void 0:c.metadata)||void 0===m?void 0:m.cta)&&h("div",{class:"bump__product-title","aria-hidden":"true"},this.bump.name||(null==g?void 0:g.name)),!!(null===(_=null===(v=this.bump)||void 0===v?void 0:v.metadata)||void 0===_?void 0:_.description)&&h("div",{class:"bump__product-description","aria-label":sprintf(
/* translators: %s: Product description */
__("Product description: %s.","surecart"),null===(b=this.bump)||void 0===b?void 0:b.rendered_description)},h("span",{"aria-hidden":"true",innerHTML:null===(f=this.bump)||void 0===f?void 0:f.rendered_description}))))))}render(){return this.isModern?this.renderModern():this.renderClassic()}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 (classic design)"},attribute:"show-control",reflect:!0}}}static get states(){return{loading:{}}}static get elementRef(){return"el"}}