import{proxyCustomElement,HTMLElement,h}from"@stencil/core/internal/client";import{s as state}from"./mutations2.js";import{i as isRtl}from"./page-align.js";import{f as formBusy}from"./getters3.js";import{d as createOrUpdateCheckout}from"./index4.js";import{u as updateFormState}from"./mutations5.js";import{s as speak}from"./index2.js";import{d as defineCustomElement$e}from"./sc-alert2.js";import{d as defineCustomElement$d}from"./sc-block-ui2.js";import{d as defineCustomElement$c}from"./sc-button2.js";import{d as defineCustomElement$b}from"./sc-coupon-form2.js";import{d as defineCustomElement$a}from"./sc-form-control2.js";import{d as defineCustomElement$9}from"./sc-format-number2.js";import{d as defineCustomElement$8}from"./sc-icon2.js";import{d as defineCustomElement$7}from"./sc-input2.js";import{d as defineCustomElement$6}from"./sc-line-item2.js";import{d as defineCustomElement$5}from"./sc-skeleton2.js";import{d as defineCustomElement$4}from"./sc-spinner2.js";import{d as defineCustomElement$3}from"./sc-tag2.js";import{d as defineCustomElement$2}from"./sc-visually-hidden2.js";const scOrderCouponFormCss=":host{display:block}.coupon-form{position:relative}.form{opacity:0;visibility:hidden;height:0;transition:opacity var(--sc-transition-fast) ease-in-out}.coupon-form--is-open .form{opacity:1;visibility:visible;height:auto;margin-top:var(--sc-spacing-small);display:grid;gap:var(--sc-spacing-small)}.coupon-form--is-open .trigger{color:var(--sc-input-label-color)}.coupon-form--is-open .trigger:hover{text-decoration:none}.trigger{cursor:pointer;font-size:var(--sc-font-size-small);color:var(--sc-color-gray-500);user-select:none}.trigger:hover{text-decoration:underline}.order-coupon-form--is-rtl .trigger,.order-coupon-form--is-rtl .trigger:hover{text-align:right}",ScOrderCouponFormStyle0=scOrderCouponFormCss,ScOrderCouponForm$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),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 o,t,s,n,r;const i=(null==e?void 0:e.detail)||null;try{this.error=null,updateFormState("FETCH"),state.checkout=await createOrUpdateCheckout({id:state.checkout.id,data:{discount:{...i?{promotion_code:i}:{}}}}),i&&speak(
// translators: %1$s is the coupon code, %2$s is the discount amount, %3$s is the order total
wp.i18n.sprintf(wp.i18n.__("Coupon code %1$s applied, discount is %2$s and now order total is %3$s","surecart"),i,null===(o=state.checkout)||void 0===o?void 0:o.discounts_display_amount,null===(t=state.checkout)||void 0===t?void 0:t.total_display_amount),"assertive"),updateFormState("RESOLVE"),await(null===(s=this.couponForm)||void 0===s?void 0:s.triggerFocus())}catch(e){console.error(e),this.error=(null===(r=null===(n=null==e?void 0:e.additional_errors)||void 0===n?void 0:n[0])||void 0===r?void 0:r.message)||(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart"),updateFormState("REJECT")}}render(){var e,o,t,s,n,r,i,c,l,m;const a=null===(t=null===(o=null===(e=null==state?void 0:state.checkout)||void 0===e?void 0:e.line_items)||void 0===o?void 0:o.data)||void 0===t?void 0:t.some((e=>{var o;return null===(o=null==e?void 0:e.price)||void 0===o?void 0:o.recurring_interval}));return h("sc-coupon-form",{key:"a6899ee676c036b0e1e8925f098eed882a2ebe32",ref:e=>this.couponForm=e,label:this.label||wp.i18n.__("Add Coupon Code","surecart"),collapsed:this.collapsed,placeholder:this.placeholder,loading:formBusy()&&!(null===(r=null===(n=null===(s=state.checkout)||void 0===s?void 0:s.line_items)||void 0===n?void 0:n.data)||void 0===r?void 0:r.length),busy:formBusy(),discount:null===(i=state.checkout)||void 0===i?void 0:i.discount,"discounts-display-amount":null===(c=state.checkout)||void 0===c?void 0:c.discounts_display_amount,currency:null===(l=state.checkout)||void 0===l?void 0:l.currency,"discount-amount":null===(m=state.checkout)||void 0===m?void 0:m.discount_amount,class:{"order-coupon-form--is-rtl":isRtl()},"button-text":this.buttonText||wp.i18n.__("Apply","surecart"),"show-interval":a,onScApplyCoupon:e=>this.handleCouponApply(e),error:this.error})}static get style(){return ScOrderCouponFormStyle0}},[1,"sc-order-coupon-form",{label:[1],loading:[4],collapsed:[4],placeholder:[1],buttonText:[1,"button-text"],open:[32],value:[32],error:[32]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-order-coupon-form","sc-alert","sc-block-ui","sc-button","sc-coupon-form","sc-form-control","sc-format-number","sc-icon","sc-input","sc-line-item","sc-skeleton","sc-spinner","sc-tag","sc-visually-hidden"].forEach((e=>{switch(e){case"sc-order-coupon-form":customElements.get(e)||customElements.define(e,ScOrderCouponForm$1);break;case"sc-alert":customElements.get(e)||defineCustomElement$e();break;case"sc-block-ui":customElements.get(e)||defineCustomElement$d();break;case"sc-button":customElements.get(e)||defineCustomElement$c();break;case"sc-coupon-form":customElements.get(e)||defineCustomElement$b();break;case"sc-form-control":customElements.get(e)||defineCustomElement$a();break;case"sc-format-number":customElements.get(e)||defineCustomElement$9();break;case"sc-icon":customElements.get(e)||defineCustomElement$8();break;case"sc-input":customElements.get(e)||defineCustomElement$7();break;case"sc-line-item":customElements.get(e)||defineCustomElement$6();break;case"sc-skeleton":customElements.get(e)||defineCustomElement$5();break;case"sc-spinner":customElements.get(e)||defineCustomElement$4();break;case"sc-tag":customElements.get(e)||defineCustomElement$3();break;case"sc-visually-hidden":customElements.get(e)||defineCustomElement$2()}}))}const ScOrderCouponForm=ScOrderCouponForm$1,defineCustomElement=defineCustomElement$1;export{ScOrderCouponForm,defineCustomElement};