'use strict';
import { withIdProps } from '../../../fd-toolbox-core/types/resource-with-id.js';
import { tryGetObjectFromCoreCache, setCoreCache } from '../../../fd-toolbox/server/cachings/core-cache.js';
import { baseResourceProps } from '../../../fd-toolbox/base-resource/props/base-resource-props.js';
import { oDataComparisonTypes, oDataFilterTypes } from '../../../fd-toolbox/odata/odata-enums.js';
import { singleOrUndefined, firstOrUndefined } from '../../../fd-toolbox/server/collections/single-funcs.js';
import { ensureDateStringInIso } from '../../../fd-toolbox/dates/date-time.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { logWarning } from '../../../fd-toolbox/server/logging/logger.js';
import { crudResultTypes } from '../../../fd-toolbox/types/crud/crud-result-type.js';
import { httpStatusCodes } from '../../../fd-toolbox/constants/http-status-codes.js';
import { getCurrentWorkspaceId } from '../../meta/server/providers/current-workspace-id-provider.js';
import { workspaceResourceProps } from '../../meta/multitenancy/props/workspace-resource-props.js';
import { getInternalResourceMeta } from '../../meta/server/resources/internal-resource-meta.js';
import { pricingProps } from '../../meta/core/resources/props/payment-plan-price-props.js';
import { getResourcesByFieldId, getResourcesWithOData } from '../../meta/server/db/db.js';
import { subscriptionStatuses } from '../../meta/core/resources/enums/payment-enums.js';
import { usageLimitTypes } from '../../meta/core/resources/enums/usage-limit-enums.js';
import { usageLimitProps } from '../../meta/core/resources/props/usage-limit-props.js';
import { createValidationDetailResult } from '../../meta/server/validations/meta-validations.js';
import { getSubscriptionsByWorkspaceId } from '../payments/subscriptions/subscription-db.js';

var __defProp=Object['defineProperty'],__name=(_0x5f2c8e,_0x4cf9df)=>__defProp(_0x5f2c8e,'name',{'value':_0x4cf9df,'configurable':!![]});async function isResourceCreationLimitReached(_0x18a9de,_0x4e0cda){const _0x338902={'resourceName':_0x18a9de},_0x20a979=await checkUsageLimit(_0x338902);if(_0x20a979?.['limitReached']){_0x4e0cda['type']=crudResultTypes['notValid'];const _0x464fb2=createValidationDetailResult('Limit\x20Reached','You\x20have\x20reached\x20the\x20monthly\x20limit\x20for\x20creating\x20this\x20resource\x20on\x20your\x20current\x20plan.',_0x18a9de,httpStatusCodes['forbidden']);return _0x4e0cda['validationResult']={'isValid':![],'details':[_0x464fb2]},!![];}return ![];}__name(isResourceCreationLimitReached,'isResourceCreationLimitReached');async function checkUsageLimit(_0x10dfad){const _0x2d3471=await getInternalResourceMeta(_0x10dfad['resourceName']),_0x5e4fd2=await getCurrentWorkspaceId();let _0x25107a;if(_0x5e4fd2){const _0x5ba1b7=await getSubscriptionsByWorkspaceId(_0x5e4fd2),_0x5f180c=getActiveSubscription(_0x5ba1b7);if(_0x5f180c?.['pricingId']){const _0x20c97c=await getPricingResourcesByFieldId(_0x5f180c['pricingId']);if(_0x20c97c?.['paymentPlanId']){const _0x1fdae4=await getUsageLimitWithCache(_0x2d3471['id'],_0x20c97c['paymentPlanId']);_0x1fdae4&&isMonthlyResourceCreationLimit(_0x1fdae4)&&(_0x25107a=await checkMonthlyResourceCreationLimit(_0x1fdae4,_0x10dfad['resourceName'],_0x5e4fd2,_0x5f180c));}}}return _0x25107a;}__name(checkUsageLimit,'checkUsageLimit');function getActiveSubscription(_0x59f8f1){const _0x4180bb=_0x59f8f1['find'](_0x55982a=>_0x55982a['status']===subscriptionStatuses['active']||_0x55982a['status']===subscriptionStatuses['trialing']);return _0x4180bb;}__name(getActiveSubscription,'getActiveSubscription');async function getPricingResourcesByFieldId(_0x223e5e){const _0x2ebb64=await getResourcesByFieldId(resourceNames['pricing'],pricingProps['id'],_0x223e5e);return singleOrUndefined(_0x2ebb64);}__name(getPricingResourcesByFieldId,'getPricingResourcesByFieldId');async function getUsageLimitWithCache(_0x943031,_0x546df8){const _0xbad6dd='usage-limit_'+_0x943031+'_'+_0x546df8;let _0x9b148c=await tryGetObjectFromCoreCache(_0xbad6dd);if(_0x9b148c)return _0x9b148c;const _0x719a03={'filters':[{'leftSide':usageLimitProps['resourceId'],'rightSide':_0x943031,'type':oDataFilterTypes['guid'],'comparisonType':oDataComparisonTypes['equal']},{'leftSide':usageLimitProps['paymentPlanId'],'rightSide':_0x546df8,'type':oDataFilterTypes['guid'],'comparisonType':oDataComparisonTypes['equal']}]},_0x552e5b=await getResourcesWithOData(resourceNames['usageLimit'],_0x719a03);return _0x552e5b['length']>-0x222c+-0x1*-0x12c1+-0x234*-0x7&&logWarning('Multiple\x20usage\x20limits\x20found\x20for\x20resource\x20'+_0x943031+'\x20and\x20plan\x20'+_0x546df8),_0x9b148c=firstOrUndefined(_0x552e5b),_0x9b148c&&await setCoreCache(_0xbad6dd,_0x9b148c,{'absoluteExpirationRelativeToNowSeconds':0x2a30}),_0x9b148c;}__name(getUsageLimitWithCache,'getUsageLimitWithCache');async function checkMonthlyResourceCreationLimit(_0x4bc1ea,_0x3231c1,_0x49254e,_0x56a282){const _0x2c98aa=_0x56a282['currentPeriodStartDate'];if(!_0x2c98aa)return void(-0x2*0xd83+0xe0+0x1*0x1a26);const _0x4deb9e=_0x4bc1ea['numericLimitValue'],_0x4e9f72={'filters':[{'leftSide':workspaceResourceProps['workspaceId'],'rightSide':_0x49254e,'type':oDataFilterTypes['guid'],'comparisonType':oDataComparisonTypes['equal']},{'leftSide':baseResourceProps['createdDate'],'rightSide':ensureDateStringInIso(_0x2c98aa),'type':oDataFilterTypes['date'],'comparisonType':oDataComparisonTypes['greaterThanOrEqual']}],'top':_0x4deb9e,'selects':[withIdProps['id']]},_0x47fb60=await getResourcesWithOData(_0x3231c1,_0x4e9f72);if(_0x47fb60['length']>=_0x4deb9e){const _0x4e0182={'limitReached':!![]};return _0x4e0182;}return void(0xa5*0x3b+0x192+-0x2799);}__name(checkMonthlyResourceCreationLimit,'checkMonthlyResourceCreationLimit');function isMonthlyResourceCreationLimit(_0x3463d7){return _0x3463d7['type']===usageLimitTypes['monthlyResourceCreationLimit'];}__name(isMonthlyResourceCreationLimit,'isMonthlyResourceCreationLimit');

export { isResourceCreationLimitReached };
