import{Host,h,Fragment}from"@stencil/core";import{__}from"@wordpress/i18n";import{intervalString}from"../../../../functions/price";import{getLineItemByProductId}from"../../../../store/checkout/getters";import{formBusy}from"../../../../store/form/getters";import{onChange}from"../../../../store/checkout/index";export class ScProductSelectedPrice{constructor(){this.productId=void 0,this.showInput=void 0,this.adHocAmount=void 0}lineItem(){return getLineItemByProductId(this.productId)}componentWillLoad(){onChange("checkout",(()=>{var t,e,i;this.adHocAmount=(null===(t=this.lineItem())||void 0===t?void 0:t.ad_hoc_amount)||(null===(i=null===(e=this.lineItem())||void 0===e?void 0:e.price)||void 0===i?void 0:i.amount)}))}updatePrice(){var t,e,i;this.showInput=!1,(this.adHocAmount||0===this.adHocAmount)&&this.adHocAmount!==(null===(t=this.lineItem())||void 0===t?void 0:t.ad_hoc_amount)&&this.scUpdateLineItem.emit({price_id:null===(i=null===(e=this.lineItem())||void 0===e?void 0:e.price)||void 0===i?void 0:i.id,quantity:1,ad_hoc_amount:this.adHocAmount})}handleShowInputChange(t){t&&setTimeout((()=>{this.input.triggerFocus()}),50)}onSubmit(t){t.preventDefault(),t.stopImmediatePropagation(),this.updatePrice()}render(){var t,e,i,o,r,n;const c=null===(t=this.lineItem())||void 0===t?void 0:t.price,s=null===(e=this.lineItem())||void 0===e?void 0:e.variant;return c?h("div",{class:{"selected-price":!0}},this.showInput?h("sc-form",{onScSubmit:t=>this.onSubmit(t),onScFormSubmit:t=>{t.preventDefault(),t.stopImmediatePropagation()}},h("sc-price-input",{ref:t=>this.input=t,size:"large","currency-code":(null==c?void 0:c.currency)||"usd",min:null==c?void 0:c.ad_hoc_min_amount,max:null==c?void 0:c.ad_hoc_max_amount,placeholder:"0.00",required:!0,value:null===(o=null===(i=this.adHocAmount)||void 0===i?void 0:i.toString)||void 0===o?void 0:o.call(i),onScInput:t=>this.adHocAmount=parseFloat(t.target.value),onKeyDown:t=>{"Enter"===t.key&&this.onSubmit(t)}},h("sc-button",{slot:"suffix",type:"link",submit:!0},__("Update","surecart")))):h(Fragment,null,h("div",{class:"selected-price__wrap"},h("span",{class:"selected-price__price","aria-label":__("Product price","surecart")},(null==c?void 0:c.scratch_amount)>c.amount&&h(Fragment,null,h("sc-format-number",{class:"selected-price__scratch-price",part:"price__scratch",type:"currency",currency:null==c?void 0:c.currency,value:null==c?void 0:c.scratch_amount})," "),h("sc-format-number",{type:"currency",currency:null==c?void 0:c.currency,value:null!==(null===(r=this.lineItem())||void 0===r?void 0:r.ad_hoc_amount)?null===(n=this.lineItem())||void 0===n?void 0:n.ad_hoc_amount:(null==s?void 0:s.amount)||(null==c?void 0:c.amount)})),h("span",{class:"selected-price__interval","aria-label":__("Price interval","surecart")},intervalString(c,{labels:{interval:"/",period:
/** translators: used as in time period: "for 3 months" */
__("for","surecart")}}))),(null==c?void 0:c.ad_hoc)&&!formBusy()&&h("sc-button",{class:"selected-price__change-amount",type:"primary",size:"small",onClick:()=>this.showInput=!0},h("sc-icon",{name:"edit",slot:"prefix"}),__("Change Amount","surecart")))):h(Host,{style:{display:"none"}})}static get is(){return"sc-product-selected-price"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-product-selected-price.scss"]}}static get styleUrls(){return{$:["sc-product-selected-price.css"]}}static get properties(){return{productId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The product id."},attribute:"product-id",reflect:!1}}}static get states(){return{showInput:{},adHocAmount:{}}}static get events(){return[{method:"scUpdateLineItem",name:"scUpdateLineItem",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Toggle line item event"},complexType:{original:"LineItemData",resolved:"LineItemData",references:{LineItemData:{location:"import",path:"src/types",id:"src/types.ts::LineItemData"}}}}]}static get watchers(){return[{propName:"showInput",methodName:"handleShowInputChange"}]}}