'use strict';
import _0x14f4e7 from 'stripe';
import { userFacingErrorMessages, paymentErrorMessages } from '../../common/payments/payment-constants.js';
import { paymentPlanProps } from '../../meta/core/resources/props/payment-plan-props.js';
import { httpStatusCodes } from '../../../fd-toolbox/constants/http-status-codes.js';
import { deactivatePaymentPlan, changePaymentSystemPlanName, changePaymentSystemPlanStatus } from './payments/payments.js';
import { createValidationDetailResult } from '../../meta/server/validations/meta-validations.js';
import { getResource } from '../../meta/server/db/db.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { createStripeProduct, createStripeProductPricing } from './stripe/stripes.js';
import { crudUpdate } from '../../meta/server/basic-crud/cruds.js';
import { getUserId } from '../../meta/server/auth/users/current-user-id-provider.js';
import { createInvalidValidationResult } from '../../../fd-toolbox/types/crud/crud-results.js';

var __defProp=Object['defineProperty'],__name=(_0x2fbf3d,_0x517f70)=>__defProp(_0x2fbf3d,'name',{'value':_0x517f70,'configurable':!![]});async function preUpdate(_0x564cd7){const _0x380bb3=_0x564cd7['modifiedResource'],_0x2525d9=_0x564cd7['originalResource'],_0x495798=_0x564cd7['crudUpdateConfig'];_0x495798['isCreate']&&await syncNewPaymentPlanWithPaymentSystem(_0x380bb3,_0x495798),await syncExistingPaymentPlanWithPaymentSystem(_0x380bb3,_0x564cd7,_0x2525d9);}__name(preUpdate,'preUpdate');async function afterDelete(_0x2bc0ed){await deactivatePaymentPlan(_0x2bc0ed['deleteResource']['name']);}__name(afterDelete,'afterDelete');async function syncExistingPaymentPlanWithPaymentSystem(_0x4ec0fe,_0x9d96ab,_0xaef8a1){if(_0xaef8a1?.['paymentSystemId']&&_0x4ec0fe['paymentSystemId']){const _0x1aeba8=_0x4ec0fe['name']!==_0xaef8a1['name'],_0x420c4d=_0x4ec0fe['isActive']!==_0xaef8a1['isActive'];if(_0x1aeba8||_0x420c4d){const _0x1a2f16=await tryChangePaymentPlanName(_0x4ec0fe['paymentSystemId'],_0x4ec0fe['name'],_0x1aeba8,_0x9d96ab);_0x1a2f16&&_0x420c4d&&await tryChangePaymentPlanStatus(_0x4ec0fe['paymentSystemId'],_0x4ec0fe['isActive'],_0x9d96ab);}}}__name(syncExistingPaymentPlanWithPaymentSystem,'syncExistingPaymentPlanWithPaymentSystem');async function syncNewPaymentPlanWithPaymentSystem(_0x2660d0,_0x38b4e8){const _0x2c5450=await createStripeProduct(_0x2660d0['name'],_0x2660d0['id']);_0x2660d0['paymentSystemId']=_0x2c5450['id'];if(_0x38b4e8['propertyRelationSyncInfos'])for(const _0x490653 of _0x38b4e8['propertyRelationSyncInfos']){if(_0x490653['propertyName']===paymentPlanProps['pricingList'])for(const _0x4189fe of _0x490653['relatedResourceIds']){const _0x2cd738=await getResource({'resourceName':resourceNames['pricing'],'id':_0x4189fe}),_0x1fca33=await createStripeProductPricing(_0x2c5450['id'],_0x2cd738['currency'],_0x2cd738['price'],_0x2cd738['id'],_0x2cd738['billingInterval']);await crudUpdate({'resourceName':resourceNames['pricing'],'resource':{..._0x2cd738,'paymentSystemPriceId':_0x1fca33['id']},'userId':await getUserId()});}}}__name(syncNewPaymentPlanWithPaymentSystem,'syncNewPaymentPlanWithPaymentSystem');async function tryChangePaymentPlanName(_0x198d61,_0x20d6ca,_0x33130e,_0x32166d){let _0x5e7916=!![];try{_0x33130e&&await changePaymentSystemPlanName(_0x198d61,_0x20d6ca);}catch(_0x48d7c1){processError(_0x48d7c1,userFacingErrorMessages['paymentPlanNameChangeError'],paymentErrorMessages['paymentPlanNameChangeError'],paymentPlanProps['name'],_0x32166d),_0x5e7916=![];}return _0x5e7916;}__name(tryChangePaymentPlanName,'tryChangePaymentPlanName');async function tryChangePaymentPlanStatus(_0x4c3eb3,_0x35763a,_0x3541f1){try{await changePaymentSystemPlanStatus(_0x4c3eb3,_0x35763a);}catch(_0x5ab323){processError(_0x5ab323,userFacingErrorMessages['productPriceIdNotFound'],paymentErrorMessages['productPriceIdNotFound'],paymentPlanProps['isActive'],_0x3541f1);}}__name(tryChangePaymentPlanStatus,'tryChangePaymentPlanStatus');function processError(_0x31cc78,_0x199401,_0x59a43a,_0x306d38,_0x418bdf){if(_0x31cc78 instanceof _0x14f4e7['errors']['StripeError']||_0x31cc78 instanceof Error){const _0x834be8=_0x31cc78 instanceof _0x14f4e7['errors']['StripeError']?httpStatusCodes['badRequest']:httpStatusCodes['internalServerError'],_0x12656a=createValidationDetailResult(_0x59a43a,_0x199401,_0x306d38,_0x834be8);_0x418bdf['crudServiceResult']['validationResult']=createInvalidValidationResult([_0x12656a]);}}__name(processError,'processError');

export { afterDelete as afterDeletePaymentPlan, preUpdate as preUpdatePaymentPlan };
