import{h}from"@stencil/core";import{state as checkoutState}from"../../../../store/checkout/index";import{__,sprintf}from"@wordpress/i18n";import{isRtl}from"../../../../functions/page-align";import{formBusy}from"../../../../store/form/getters";import{createOrUpdateCheckout}from"../../../../services/session/index";import{updateFormState}from"../../../../store/form/mutations";import{speak}from"@wordpress/a11y";export class ScOrderCouponForm{constructor(){this.label=void 0,this.loading=void 0,this.collapsed=void 0,this.placeholder=void 0,this.buttonText=void 0,this.open=void 0,this.value=void 0,this.error=void 0}async handleCouponApply(e){var t,o,r,i,l;const s=(null==e?void 0:e.detail)||null;try{this.error=null,updateFormState("FETCH"),checkoutState.checkout=await createOrUpdateCheckout({id:checkoutState.checkout.id,data:{discount:{...s?{promotion_code:s}:{}}}}),s&&speak(
// translators: %1$s is the coupon code, %2$s is the discount amount, %3$s is the order total
sprintf(__("Coupon code %1$s applied, discount is %2$s and now order total is %3$s","surecart"),s,null===(t=checkoutState.checkout)||void 0===t?void 0:t.discounts_display_amount,null===(o=checkoutState.checkout)||void 0===o?void 0:o.total_display_amount),"assertive"),updateFormState("RESOLVE"),await(null===(r=this.couponForm)||void 0===r?void 0:r.triggerFocus())}catch(e){console.error(e),this.error=(null===(l=null===(i=null==e?void 0:e.additional_errors)||void 0===i?void 0:i[0])||void 0===l?void 0:l.message)||(null==e?void 0:e.message)||__("Something went wrong","surecart"),updateFormState("REJECT")}}render(){var e,t,o,r,i,l,s,a,c,n;const u=null===(o=null===(t=null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.line_items)||void 0===t?void 0:t.data)||void 0===o?void 0:o.some((e=>{var t;return null===(t=null==e?void 0:e.price)||void 0===t?void 0:t.recurring_interval}));return h("sc-coupon-form",{key:"a6899ee676c036b0e1e8925f098eed882a2ebe32",ref:e=>this.couponForm=e,label:this.label||__("Add Coupon Code","surecart"),collapsed:this.collapsed,placeholder:this.placeholder,loading:formBusy()&&!(null===(l=null===(i=null===(r=checkoutState.checkout)||void 0===r?void 0:r.line_items)||void 0===i?void 0:i.data)||void 0===l?void 0:l.length),busy:formBusy(),discount:null===(s=checkoutState.checkout)||void 0===s?void 0:s.discount,"discounts-display-amount":null===(a=checkoutState.checkout)||void 0===a?void 0:a.discounts_display_amount,currency:null===(c=checkoutState.checkout)||void 0===c?void 0:c.currency,"discount-amount":null===(n=checkoutState.checkout)||void 0===n?void 0:n.discount_amount,class:{"order-coupon-form--is-rtl":isRtl()},"button-text":this.buttonText||__("Apply","surecart"),"show-interval":u,onScApplyCoupon:e=>this.handleCouponApply(e),error:this.error})}static get is(){return"sc-order-coupon-form"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-order-coupon-form.scss"]}}static get styleUrls(){return{$:["sc-order-coupon-form.css"]}}static get properties(){return{label:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"label",reflect:!1},loading:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"loading",reflect:!1},collapsed:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"collapsed",reflect:!1},placeholder:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"placeholder",reflect:!1},buttonText:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"button-text",reflect:!1}}}static get states(){return{open:{},value:{},error:{}}}}