import{h}from"@stencil/core";import{state as checkoutState}from"../../../../store/checkout/index";import{__}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";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;const i=(null==e?void 0:e.detail)||null;try{this.error=null,updateFormState("FETCH"),checkoutState.checkout=await createOrUpdateCheckout({id:checkoutState.checkout.id,data:{discount:{...i?{promotion_code:i}:{}}}}),updateFormState("RESOLVE"),await(null===(t=this.couponForm)||void 0===t?void 0:t.triggerFocus())}catch(e){console.error(e),this.error=(null===(r=null===(o=null==e?void 0:e.additional_errors)||void 0===o?void 0:o[0])||void 0===r?void 0:r.message)||(null==e?void 0:e.message)||__("Something went wrong","surecart"),updateFormState("REJECT")}}render(){var e,t,o,r,i,l,s,c,n,a;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:"65db088edebe3f3ecc89d9ab038c585c4b0b30df",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===(c=checkoutState.checkout)||void 0===c?void 0:c.discounts_display_amount,currency:null===(n=checkoutState.checkout)||void 0===n?void 0:n.currency,"discount-amount":null===(a=checkoutState.checkout)||void 0===a?void 0:a.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:{}}}}