'use strict';
import { z } from 'zod';
import { notifyFailure, notifySuccess, notifyWarning } from '../../../../fd-toolbox/notifications.js';
import { apiPost, apiPut } from '../../../../fd-toolbox/api/api-client.js';
import { apiPathNames } from '../../../../fd-toolbox/api/api-path-names.js';
import { navigateWithReload } from '../../../../fd-toolbox/routing/routers.js';
import { routes } from '../../../../fd-toolbox/routing/routes.js';
import { paymentMessages, paymentErrors } from '../../../common/payments/payment-constants.js';
import { isErrorResponseWithDetail } from '../../../../fd-toolbox/types/ensure-type.js';
import { paymentStatus, stripeErrorConstants } from '../../../common/payments/stripe/stripe-constants.js';
import { CardElement } from '@stripe/react-stripe-js';
import { formMessages } from '../../../fd-app-toolbox/constants/form-constants.js';
import { setBodyCursorBusy } from '../../../../fd-toolbox/html/document-body-cursor.js';

var __defProp=Object['defineProperty'],__name=(_0x212870,_0x1c223f)=>__defProp(_0x212870,'name',{'value':_0x1c223f,'configurable':!![]});const schema=z['object']({'email':z['string']()['email'](formMessages['invalidEmailAdress']),'name':z['string']()['min'](-0x25d5+0x328+0x22ae,'Cardholder\x20name\x20is\x20required'),'country':z['string']()['min'](-0x137d+0x12cd+0xb1,'Country\x20name\x20is\x20required'),'zip':z['string']()['min'](0x2*0xa8b+-0x1120+-0x3f2,'ZIP\x20code\x20must\x20be\x20at\x20least\x204\x20characters')['max'](-0x1*-0xfb+-0x5c+-0x15*0x7,'ZIP\x20code\x20must\x20be\x20at\x20most\x2012\x20characters')});function updateCursorOnFormSubmit(_0x467c8d,_0x3883c9){setBodyCursorBusy(_0x467c8d),_0x3883c9(_0x467c8d);}__name(updateCursorOnFormSubmit,'updateCursorOnFormSubmit');async function attachPaymentMethodWithSubscription(_0x25c897,_0x504426,_0x2f02cd,_0x833011,_0x3e3fa9,_0x59e8ba,_0x3bf04c){const _0x56fbaa=_0x833011?.['getElement'](CardElement);let _0x258f9e;_0x3e3fa9&&_0x56fbaa&&(_0x258f9e=await processCheckoutFormSubscriptionAttachment(_0x25c897,_0x504426,_0x3e3fa9,_0x56fbaa)),_0x258f9e&&(_0x2f02cd(),processCheckoutFormPaymentResult(_0x258f9e,_0x59e8ba,_0x3bf04c));}__name(attachPaymentMethodWithSubscription,'attachPaymentMethodWithSubscription');async function processCheckoutFormSubscriptionAttachment(_0x384b8d,_0x400810,_0x3bef8a,_0x567482){const _0x5ab4dc=createCheckoutFormPaymentMethodParams({..._0x384b8d,'card':_0x567482}),{error:_0x3874ed,paymentMethod:_0x5e48fa}=await _0x3bef8a['createPaymentMethod'](_0x5ab4dc);let _0x19f4b2;if(_0x3874ed)notifyFailure(_0x3874ed['message']);else {const _0x1f66b8={'name':_0x384b8d['name'],'email':_0x384b8d['email'],'pricingId':_0x400810,'paymentMethodId':_0x5e48fa['id']};_0x19f4b2=await processCheckoutFormSubscriptionPayment(_0x1f66b8);if(_0x19f4b2&&!_0x19f4b2['isPaymentCompleted']&&_0x19f4b2['clientSecret']&&_0x19f4b2['subscriptionId']){const _0x3c5c5c=await _0x3bef8a['confirmCardPayment'](_0x19f4b2['clientSecret'],{'payment_method':_0x5e48fa['id']});_0x3c5c5c['paymentIntent']?.['status']!==paymentStatus['succeeded']?(await processCheckoutFormFailedConfirmation(_0x19f4b2['subscriptionId']),_0x19f4b2=void(0x19a4+0x2444+-0x1ef4*0x2)):_0x19f4b2=await processCheckoutFormSuccessfulConfirmation(_0x3c5c5c['paymentIntent']['id'],_0x400810);}}return _0x19f4b2;}__name(processCheckoutFormSubscriptionAttachment,'processCheckoutFormSubscriptionAttachment');async function attachPaymentMethodWithoutSubscription(_0x3b6e40,_0x5e4b2d,_0x2d45db,_0x4b4bae){const _0x5c01f9=_0x4b4bae?.['getElement'](CardElement);let _0x2ac7ea=![];if(_0x2d45db&&_0x5c01f9){const _0xe3a5eb=createCheckoutFormPaymentMethodParams({..._0x3b6e40,'card':_0x5c01f9}),{error:_0x904d02,paymentMethod:_0x330a1d}=await _0x2d45db['createPaymentMethod'](_0xe3a5eb);_0x904d02?notifyFailure(_0x904d02['message']):(await attachPaymentMethodToUser(_0x330a1d['id']),_0x2ac7ea=!![]);}_0x2ac7ea&&_0x5e4b2d();}__name(attachPaymentMethodWithoutSubscription,'attachPaymentMethodWithoutSubscription');function onCardFieldChange(_0xfa2539,_0x1beeda,_0x5b1272){_0x1beeda(_0xfa2539['complete']),_0x5b1272(!!_0xfa2539['error']);}__name(onCardFieldChange,'onCardFieldChange');function handleFormSubmit(_0x16ff41,_0xfd39b,_0x94f4c5,_0x4f532a,_0x450d28,_0x58b5ee,_0x294318){let _0x59bcd7;return _0x94f4c5?_0x59bcd7=attachPaymentMethodWithSubscription(_0x16ff41,_0x94f4c5,_0xfd39b,_0x4f532a,_0x450d28,_0x58b5ee,_0x294318):_0x59bcd7=attachPaymentMethodWithoutSubscription(_0x16ff41,_0xfd39b,_0x450d28,_0x4f532a),_0x59bcd7;}__name(handleFormSubmit,'handleFormSubmit');function processCheckoutFormPaymentResult(_0x86e897,_0x1c5961,_0x120db6=routes['contact']){if(!_0x86e897?.['isPaymentCompleted'])return;notifySuccess(paymentMessages['paymentSuccessful']),_0x1c5961&&navigateWithReload(_0x120db6);}__name(processCheckoutFormPaymentResult,'processCheckoutFormPaymentResult');function createCheckoutFormPaymentMethodParams(_0x4f8df3){const _0x3a6088={'type':'card','card':_0x4f8df3['card'],'billing_details':{'name':_0x4f8df3['name'],'email':_0x4f8df3['email'],'address':{'postal_code':_0x4f8df3['zip'],'country':_0x4f8df3['country']}}};return _0x3a6088;}__name(createCheckoutFormPaymentMethodParams,'createCheckoutFormPaymentMethodParams');async function processCheckoutFormSubscriptionPayment(_0x959301){const _0xebacfa=await apiPost(apiPathNames['payment'],_0x959301),_0x10afb8=paymentErrors['emailAlreadyUsed']['split']('{0}'),[_0x29241f,_0x3b745a]=_0x10afb8;if(isErrorResponseWithDetail(_0xebacfa)&&_0xebacfa['detail']['includes'](_0x29241f)&&_0xebacfa['detail']['includes'](_0x3b745a))return notifyWarning(stripeErrorConstants['emailAlreadyInUse']),void(0x1e6*0x11+-0xf31*0x1+0x1115*-0x1);return _0xebacfa['data'];}__name(processCheckoutFormSubscriptionPayment,'processCheckoutFormSubscriptionPayment');async function processCheckoutFormFailedConfirmation(_0x6082f4){await apiPost(apiPathNames['paymentVerifyFail'],{'subscriptionId':_0x6082f4});}__name(processCheckoutFormFailedConfirmation,'processCheckoutFormFailedConfirmation');async function attachPaymentMethodToUser(_0x38ec3e){return apiPut(apiPathNames['paymentMethod'],{'paymentMethodId':_0x38ec3e});}__name(attachPaymentMethodToUser,'attachPaymentMethodToUser');async function processCheckoutFormSuccessfulConfirmation(_0x15d7c5,_0x354ace){const _0x42a495={'paymentIntentId':_0x15d7c5,'planId':_0x354ace},_0x57f9eb=await apiPost(apiPathNames['paymentVerifySuccess'],_0x42a495);return _0x57f9eb['data'];}__name(processCheckoutFormSuccessfulConfirmation,'processCheckoutFormSuccessfulConfirmation');

export { attachPaymentMethodWithSubscription, attachPaymentMethodWithoutSubscription, handleFormSubmit, onCardFieldChange, schema, updateCursorOnFormSubmit };
