'use strict';
import { apiGetWithOData, apiPost } from '../../../../fd-toolbox/api/api-client.js';
import { apiPathNames } from '../../../../fd-toolbox/api/api-path-names.js';
import { oDataOperators } from '../../../../fd-toolbox/enums/enums.js';
import { subscriptionProps } from '../../../meta/subscription-plan/subscription-props.js';
import { billingIntervals, subscriptionStatuses } from '../../../meta/core/resources/enums/payment-enums.js';
import { subscriptionButtonText, paymentPlanButtonId, paymentPlanCardId, paymentPlanNames } from '../../../common/subscription-plan/subscription-plan-constants.js';
import { withIdProps } from '../../../../fd-toolbox-core/types/resource-with-id.js';
import { convertAndFormatPrice } from '../../payments/payments.js';
import { getODataBuilder } from '../../../../fd-toolbox/odata/odata-builder-funcs.js';
import { addQueryParamsToPath } from '../../../../fd-toolbox/routing/paths.js';
import { redirectFunction, navigate } from '../../../../fd-toolbox/redirect/redirect-functions.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { pricingProps } from '../../../meta/core/resources/props/payment-plan-price-props.js';
import { subscriptionMessages } from '../../../../fd-toolbox-core/constants/subscription-messages.js';
import { hasScheduledCancelation, hasAnySubscription, createSubscription } from './subscriptions.js';
import { fetchActiveOrTrialSubscription } from '../../../meta/subscription-plan/fetch-active-or-trial-subscription.js';
import { getObjectPropertyKey } from '../../../../fd-toolbox/lib/utils.js';
import { oDataFilterTypes } from '../../../../fd-toolbox/odata/odata-enums.js';
import { getElementId } from '../../../../fd-toolbox/html/element-ids.js';

var __defProp=Object['defineProperty'],__name=(_0x5efbe0,_0x5f333d)=>__defProp(_0x5efbe0,'name',{'value':_0x5f333d,'configurable':!![]});async function fetchPricings(_0x30543d){const _0x478ffc=getPricingsOdataConfig(_0x30543d);let _0x2b2744=await apiGetWithOData(resourceNames['pricing'],_0x478ffc),_0x1b05f5=_0x2b2744['data']??[];return _0x1b05f5=_0x1b05f5['sort']((_0xf67efd,_0xe20309)=>{if(_0xf67efd['paymentPlan']?.['isContactUs'])return -0x152f+0x24*-0x7e+-0x78*-0x53;if(_0xe20309['paymentPlan']?.['isContactUs'])return -(0x1e6d*0x1+-0x1d*0x149+0x6d9);return _0xf67efd['price']-_0xe20309['price'];}),_0x1b05f5;}__name(fetchPricings,'fetchPricings');async function fetchFeaturesByIds(_0x4dc59e){if(!_0x4dc59e['length'])return [];const _0x192bdf=getFeaturesODataConfig(_0x4dc59e),_0x29da34=await apiGetWithOData(apiPathNames['feature'],_0x192bdf);let _0x5eb1a4=_0x29da34['data']??[];return _0x5eb1a4;}__name(fetchFeaturesByIds,'fetchFeaturesByIds');async function fetchPaymentPlanFeatures(){const _0x5bc3cd=getPaymentPlanFeatureODataConfig();let _0x2b19b3=await apiGetWithOData(apiPathNames['paymentPlanFeature'],_0x5bc3cd),_0x237eb8=_0x2b19b3['data']??[];const _0x467c0e=[...new Set(_0x237eb8['map'](_0x2109e1=>_0x2109e1['featureId']))],_0x157dff=await fetchFeaturesByIds(_0x467c0e),_0x59ddf8=new Map(_0x157dff['map'](_0x175aab=>[_0x175aab['id'],_0x175aab]));return _0x237eb8['map'](_0x6c224b=>({..._0x6c224b,'featureName':_0x59ddf8['get'](_0x6c224b['featureId'])?.['name']??''}));}__name(fetchPaymentPlanFeatures,'fetchPaymentPlanFeatures');function getFeaturesByPlan(_0x39a058){return _0x39a058['reduce']((_0x478c1b,{paymentPlanId:_0x5a9c7d,featureName:_0x22ba99})=>{return _0x22ba99&&(!_0x478c1b[_0x5a9c7d]&&(_0x478c1b[_0x5a9c7d]=[]),_0x478c1b[_0x5a9c7d]['push'](_0x22ba99)),_0x478c1b;},{});}__name(getFeaturesByPlan,'getFeaturesByPlan');async function cancelSubscription(_0x399e11){let _0x727280;return _0x727280=apiPost(apiPathNames['cancelSubscription'],{'id':_0x399e11}),_0x727280;}__name(cancelSubscription,'cancelSubscription');function getAdditionalInfo(_0x238604,_0x2f44e3){let _0x1239ac;if(!_0x2f44e3)return _0x1239ac;let _0x43aca8=getAdditionalInfoText(_0x2f44e3,_0x238604['id']),_0x92ca59=getAdditionalInfoDate(_0x2f44e3),_0x2d8c9f=showAdditionalInfo(_0x238604,_0x2f44e3);return _0x1239ac={'infoText':_0x43aca8,'infoDate':_0x92ca59,'shouldShowAdditionalInfo':_0x2d8c9f},_0x1239ac;}__name(getAdditionalInfo,'getAdditionalInfo');function getButtonText(_0x232412,_0x10ef7e){const _0x17eda9=_0x10ef7e?.['pricing'],_0x9be793=_0x232412['paymentPlan'];let _0x55b8fc=subscriptionButtonText['subscribe'];if(_0x9be793?.['isContactUs'])_0x55b8fc=subscriptionButtonText['contactUs'];else {if(_0x9be793&&isBasicPlan(_0x9be793['name']))_0x55b8fc=void(-0x118a+0x132e+-0x15*0x14);else {if(hasScheduledCancelation(_0x10ef7e,_0x232412['id']))_0x55b8fc=subscriptionButtonText['scheduledCancellation'];else {if(_0x17eda9){const _0x43d55d=_0x10ef7e?.['pricingId']===_0x232412['id'],_0xec2673=_0x10ef7e?.['pendingPricingId']===_0x232412['id'];_0x55b8fc=_0x43d55d||_0xec2673?subscriptionButtonText['cancel']:subscriptionButtonText['change'];}}}}return _0x55b8fc;}__name(getButtonText,'getButtonText');function getPaymentPlanButtonId(_0x144069,_0x14ba63){return getElementId(paymentPlanButtonId,_0x144069,_0x14ba63);}__name(getPaymentPlanButtonId,'getPaymentPlanButtonId');function getPaymentPlanId(_0x390d27,_0x1fc4f2){return paymentPlanCardId+_0x390d27+_0x1fc4f2;}__name(getPaymentPlanId,'getPaymentPlanId');function isBasicPlan(_0x25faef){return _0x25faef===paymentPlanNames['basic'];}__name(isBasicPlan,'isBasicPlan');async function updateSubscription(_0x14dcdd,_0x2fc3b3,_0x2c9775){await apiPost(apiPathNames['updateSubscription'],{'subscriptionId':_0x14dcdd,'pricingId':_0x2fc3b3}),_0x2c9775();}__name(updateSubscription,'updateSubscription');async function handlePlanAction(_0x1ca3f7,_0x58d810,_0x574bb8,_0x40ccc2,_0x1cafa2,_0x289d2a,_0x15fdf4,_0x5bd23b,_0x10c8f7,_0x4cf829,_0x45712e){if(isCancelAction(_0x1ca3f7,_0x10c8f7))_0x1cafa2(!![]),await cancelSubscription(_0x10c8f7['id']),_0x289d2a(_0x574bb8+(0x1f7d+-0x1a05*-0x1+-0x3981));else {if(_0x1ca3f7['paymentPlan']?.['isContactUs']&&_0x1ca3f7['paymentPlan']?.['contactUsUrl'])_0x45712e?.['push'](_0x1ca3f7['paymentPlan']['contactUsUrl']);else {if(await hasAnySubscription())await createSubscription(_0x1ca3f7['id']),_0x289d2a(_0x574bb8+(0x264+-0x20b7+0x1e54));else {if(_0x4cf829?.['doNotStartSubscriptionAndRedirect']&&_0x4cf829['redirectUrl']){const _0x33aa98=[{'name':subscriptionProps['pricingId'],'value':_0x1ca3f7['id']}],_0x2b2c78=addQueryParamsToPath(_0x4cf829['redirectUrl'],_0x33aa98);await redirectFunction(_0x2b2c78);}else _0x40ccc2(!_0x58d810),_0x15fdf4(_0x1ca3f7['price']),_0x5bd23b(_0x1ca3f7['id']);}}}}__name(handlePlanAction,'handlePlanAction');function isCancelAction(_0x3ed77b,_0x439971){return Boolean(isActivePricing(_0x3ed77b,_0x439971?.['pricing'],_0x439971?.['pendingPricingId'])&&_0x439971?.['id']&&_0x3ed77b['paymentPlan']&&!isBasicPlan(_0x3ed77b['paymentPlan']['name']));}__name(isCancelAction,'isCancelAction');async function onRefreshPricings(_0x56ad64,_0x366c23,_0x1ca84a,_0x14ebd2,_0x2e4c85,_0xfca79a){const [_0x22e44b,_0x58a102,_0x2ed268]=await Promise['all']([fetchPricings(_0xfca79a),fetchActiveOrTrialSubscription(),fetchPaymentPlanFeatures()]);_0x1ca84a(_0x58a102),_0x14ebd2(_0x22e44b),_0x2e4c85(_0x2ed268),_0x56ad64&&_0x366c23(![]);}__name(onRefreshPricings,'onRefreshPricings');function isActivePricing(_0x3c7ce4,_0x2f2d9d,_0x2d71eb){if(_0x2d71eb)return _0x2d71eb===_0x3c7ce4['id'];else return _0x3c7ce4['id']===_0x2f2d9d?.['id'];}__name(isActivePricing,'isActivePricing');function canUpdateSubscription(_0x336c5d,_0x346671){if(_0x346671)return _0x346671['pendingPricingId']?_0x346671['pendingPricingId']!==_0x336c5d:_0x346671['pricingId']!==_0x336c5d||!!_0x346671['endDate'];return ![];}__name(canUpdateSubscription,'canUpdateSubscription');function getPricingText(_0x514b93,_0x2821ad){return _0x514b93['paymentPlan']?.['isContactUs']?_0x2821ad:convertAndFormatPrice(_0x514b93['price'],0x1ad2*-0x1+0x15d0+0x2b3*0x2);}__name(getPricingText,'getPricingText');function getPricingDescripion(_0x2777cf){let _0x37e51d;const _0x3de750=getObjectPropertyKey(billingIntervals,_0x2777cf['billingInterval']);if(_0x3de750&&!_0x2777cf['paymentPlan']?.['isContactUs'])_0x37e51d=subscriptionMessages['perInterval']['replace']('{0}',_0x3de750);return _0x37e51d;}__name(getPricingDescripion,'getPricingDescripion');function handleCheckoutSuccess(_0x297762,_0x990a32,_0xcde148,_0x126501){!_0x297762&&_0x126501?.['redirectUrl']&&navigate(_0x126501['redirectUrl']),_0xcde148(),_0x990a32(![]);}__name(handleCheckoutSuccess,'handleCheckoutSuccess');async function updateSubscriptionHandler(_0x3a5097,_0x125b62,_0x5b9174,_0x200deb){if(_0x3a5097&&_0x200deb)_0x200deb['pricingId']===_0x125b62['id']?(await cancelSubscription(_0x200deb['id']),_0x5b9174()):await updateSubscription(_0x200deb['id'],_0x125b62['id'],_0x5b9174);}__name(updateSubscriptionHandler,'updateSubscriptionHandler');function getPricingsOdataConfig(_0x2828bb){const _0x132c5b=getODataBuilder();return _0x132c5b['expand'](pricingProps['paymentPlan']),_0x132c5b['filter'](withIdProps['id'],_0x2828bb,oDataOperators['nin'],{'type':oDataFilterTypes['guid']}),_0x132c5b['build']();}__name(getPricingsOdataConfig,'getPricingsOdataConfig');function getPaymentPlanFeatureODataConfig(_0x2e6bc2){const _0x122ec2=getODataBuilder();return _0x2e6bc2?.['length']&&_0x122ec2['filter'](withIdProps['id'],_0x2e6bc2,oDataOperators['in'],{'type':oDataFilterTypes['guid']}),_0x122ec2['build']();}__name(getPaymentPlanFeatureODataConfig,'getPaymentPlanFeatureODataConfig');function getFeaturesODataConfig(_0x3b5898){const _0x18a333=getODataBuilder();return _0x3b5898?.['length']&&_0x18a333['filter'](withIdProps['id'],_0x3b5898['map'](_0x25c4a2=>'guid\x27'+_0x25c4a2+'\x27')['join'](','),oDataOperators['in'],{'type':oDataFilterTypes['string']}),_0x18a333['build']();}__name(getFeaturesODataConfig,'getFeaturesODataConfig');function getAdditionalInfoText(_0x5605b5,_0x50fc20){if((_0x5605b5['pendingPricingId']||_0x5605b5['endDate'])&&_0x5605b5['pendingPricingId']!==_0x50fc20)return subscriptionMessages['activeUntil'];else return subscriptionMessages['availableAfter'];}__name(getAdditionalInfoText,'getAdditionalInfoText');function getAdditionalInfoDate(_0x3498fb){let _0x96f39;if(isTrialingSubscription(_0x3498fb['status'])&&_0x3498fb['currentPeriodEndDate']||_0x3498fb['pendingPricingId']!==_0x3498fb['pricingId'])_0x96f39=_0x3498fb['currentPeriodEndDate'];else _0x3498fb['endDate']&&(_0x96f39=_0x3498fb['endDate']);return _0x96f39;}__name(getAdditionalInfoDate,'getAdditionalInfoDate');function showAdditionalInfo(_0x1eecd4,_0x5b736a){if(_0x5b736a['endDate'])return _0x5b736a['pricingId']===_0x1eecd4['id']||_0x5b736a['pendingPricingId']===_0x1eecd4['id'];return !!(_0x5b736a['pendingPricingId']&&(_0x5b736a['pendingPricingId']===_0x1eecd4['id']||_0x5b736a['pricingId']===_0x1eecd4['id']));}__name(showAdditionalInfo,'showAdditionalInfo');function isTrialingSubscription(_0x26463f){return _0x26463f===subscriptionStatuses['trialing'];}__name(isTrialingSubscription,'isTrialingSubscription');

export { canUpdateSubscription, cancelSubscription, fetchFeaturesByIds, fetchPaymentPlanFeatures, fetchPricings, getAdditionalInfo, getButtonText, getFeaturesByPlan, getPaymentPlanButtonId, getPaymentPlanId, getPricingDescripion, getPricingText, handleCheckoutSuccess, handlePlanAction, isActivePricing, isBasicPlan, isCancelAction, onRefreshPricings, updateSubscription, updateSubscriptionHandler };
