import{proxyCustomElement,HTMLElement}from"@stencil/core/internal/client";import{s as state$1}from"./mutations2.js";import{s as state}from"./watchers5.js";import{o as onChange}from"./store3.js";import{c as currentFormState}from"./getters3.js";import{u as updateFormState}from"./mutations5.js";import{c as createErrorNotice}from"./mutations3.js";import{l as loadRazorpay}from"./razorpay.js";const ScCheckoutRazorpayPaymentProvider=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.razorpayInstance=null,this.confirming=!1}componentWillLoad(){loadRazorpay().then((t=>this.razorpayInstance=t)).catch((t=>createErrorNotice({message:t.message}))),this.unlistenToFormState=onChange("formState",(()=>{"paying"===currentFormState()&&this.confirm()}))}disconnectedCallback(){this.unlistenToFormState()}async confirm(){var t,e,o,a,r,n,i,s;if("razorpay"===(null==state?void 0:state.id)&&(null===(o=null===(e=null===(t=null==state$1?void 0:state$1.checkout)||void 0===t?void 0:t.payment_intent)||void 0===e?void 0:e.processor_data)||void 0===o?void 0:o.razorpay)&&"paid"!==(null===(a=null==state$1?void 0:state$1.checkout)||void 0===a?void 0:a.status)&&!this.confirming){this.confirming=!0;try{const{external_intent_id:t,processor_data:e,reusable:o}=(null===(r=null==state$1?void 0:state$1.checkout)||void 0===r?void 0:r.payment_intent)||{},{public_key:a,customer_id:c}=(null==e?void 0:e.razorpay)||{};if(!t||!a)return void createErrorNotice({message:wp.i18n.sprintf(wp.i18n.__("Payment gateway configuration incomplete. Please ensure Razorpay is properly configured for transactions.","surecart"))});this.razorpayInstance||(this.razorpayInstance=await loadRazorpay());const l=null===(n=null==state$1?void 0:state$1.checkout)||void 0===n?void 0:n.customer;let m={key:a,order_id:t,prefill:l?{...l.name&&{name:l.name},...l.email&&{email:l.email},...l.phone&&{contact:l.phone}}:void 0,customer_id:c,recurring:o,handler:t=>{if(null==t?void 0:t.razorpay_payment_id)return updateFormState("PAID");createErrorNotice({message:wp.i18n.__("Payment verification failed. Please contact support.","surecart")}),updateFormState("REJECT")},modal:{ondismiss:()=>{updateFormState("REJECT")}}};(null===(s=null===(i=null===window||void 0===window?void 0:window.wp)||void 0===i?void 0:i.hooks)||void 0===s?void 0:s.applyFilters)&&(m=window.wp.hooks.applyFilters("surecart_razorpay_checkout_options",m));const d=new this.razorpayInstance(m);d.on("payment.failed",(t=>{var e;createErrorNotice({message:(null===(e=null==t?void 0:t.error)||void 0===e?void 0:e.description)||wp.i18n.__("Payment failed. Please try again.","surecart")}),updateFormState("REJECT"),console.error("payment.failed",t)})),d.open()}catch(t){createErrorNotice(t),console.error(t),updateFormState("REJECT")}finally{this.confirming=!1}}}},[1,"sc-checkout-razorpay-payment-provider"]);function defineCustomElement(){"undefined"!=typeof customElements&&["sc-checkout-razorpay-payment-provider"].forEach((t=>{"sc-checkout-razorpay-payment-provider"===t&&(customElements.get(t)||customElements.define(t,ScCheckoutRazorpayPaymentProvider))}))}export{ScCheckoutRazorpayPaymentProvider as S,defineCustomElement as d};