import{__,sprintf}from"@wordpress/i18n";import{state as checkoutState}from"../../../../store/checkout/index";import{state as selectedProcessor}from"../../../../store/selected-processor/index";import{onChange as onChangeFormState}from"../../../../store/form/index";import{currentFormState}from"../../../../store/form/getters";import{updateFormState}from"../../../../store/form/mutations";import{createErrorNotice}from"../../../../store/notices/mutations";import{loadRazorpay}from"../../../../functions/razorpay";export class ScCheckoutRazorpayPaymentProvider{constructor(){this.razorpayInstance=null,this.confirming=!1}componentWillLoad(){loadRazorpay().then((t=>this.razorpayInstance=t)).catch((t=>createErrorNotice({message:t.message}))),this.unlistenToFormState=onChangeFormState("formState",(()=>{"paying"===currentFormState()&&this.confirm()}))}disconnectedCallback(){this.unlistenToFormState()}async confirm(){var t,e,o,r,a,i,n,c;if("razorpay"===(null==selectedProcessor?void 0:selectedProcessor.id)&&(null===(o=null===(e=null===(t=null==checkoutState?void 0:checkoutState.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===(r=null==checkoutState?void 0:checkoutState.checkout)||void 0===r?void 0:r.status)&&!this.confirming){this.confirming=!0;try{const{external_intent_id:t,processor_data:e,reusable:o}=(null===(a=null==checkoutState?void 0:checkoutState.checkout)||void 0===a?void 0:a.payment_intent)||{},{public_key:r,customer_id:s}=(null==e?void 0:e.razorpay)||{};if(!t||!r)return void createErrorNotice({message:sprintf(__("Payment gateway configuration incomplete. Please ensure Razorpay is properly configured for transactions.","surecart"))});this.razorpayInstance||(this.razorpayInstance=await loadRazorpay());const l=null===(i=null==checkoutState?void 0:checkoutState.checkout)||void 0===i?void 0:i.customer;let d={key:r,order_id:t,prefill:l?{...l.name&&{name:l.name},...l.email&&{email:l.email},...l.phone&&{contact:l.phone}}:void 0,customer_id:s,recurring:o,handler:t=>{if(null==t?void 0:t.razorpay_payment_id)return updateFormState("PAID");createErrorNotice({message:__("Payment verification failed. Please contact support.","surecart")}),updateFormState("REJECT")},modal:{ondismiss:()=>{updateFormState("REJECT")}}};(null===(c=null===(n=null===window||void 0===window?void 0:window.wp)||void 0===n?void 0:n.hooks)||void 0===c?void 0:c.applyFilters)&&(d=window.wp.hooks.applyFilters("surecart_razorpay_checkout_options",d));const u=new this.razorpayInstance(d);u.on("payment.failed",(t=>{var e;createErrorNotice({message:(null===(e=null==t?void 0:t.error)||void 0===e?void 0:e.description)||__("Payment failed. Please try again.","surecart")}),updateFormState("REJECT"),console.error("payment.failed",t)})),u.open()}catch(t){createErrorNotice(t),console.error(t),updateFormState("REJECT")}finally{this.confirming=!1}}}static get is(){return"sc-checkout-razorpay-payment-provider"}static get encapsulation(){return"shadow"}}