'use strict';
import { eq } from 'drizzle-orm';
import { getDbSchema } from '../../db/db-schema.js';
import { getQuery } from '../../meta/server/web/drizzle.js';
import { applyFilterToQuery, getTableColumn, runQuery } from '../../db/drizzle-queries.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { getResource, getResourcesByName } from '../../meta/server/db/db.js';
import { getODataBuilder } from '../../../fd-toolbox/odata/odata-builder-funcs.js';
import { featureProps } from '../../common/resources/props/feature-props.js';
import { getResources } from '../../meta/server/services/resource-provider.js';
import { oDataOperators } from '../../../fd-toolbox/enums/enums.js';
import { oDataComparisonTypes, oDataLogicalOperationTypes } from '../../../fd-toolbox/odata/odata-enums.js';
import { userRoleProps } from '../../meta/core/resources/users/props/user-role-props.js';
import { subscriptionProps } from '../../meta/subscription-plan/subscription-props.js';
import { subscriptionStatuses, paymentPlanFeatureStates } from '../../meta/core/resources/enums/payment-enums.js';
import { withWorkspaceIdProps } from '../../../fd-toolbox-core/server/base-resources/props/with-workspace-id-props.js';
import { getUserHomeWorkspaceId } from '../users/user-db.js';
import { featureStates, userFeatureStates, roleFeatureStates } from '../../meta/core/resources/enums/feature-enums.js';
import { createResource } from '../../meta/server/resources/crud-resources.js';
import { schemaNames, columnNames } from '../../db/constants/schema-names.js';
import { singleOrUndefined, firstOrUndefined } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x11d9ee,_0x310cc2)=>__defProp(_0x11d9ee,'name',{'value':_0x310cc2,'configurable':!![]});function getOneFeature(_0x20cf7c){return getResource({'resourceName':resourceNames['feature'],'id':_0x20cf7c});}__name(getOneFeature,'getOneFeature');function createFeatureResource(_0x4f4146){return createResource(_0x4f4146,resourceNames['feature']);}__name(createFeatureResource,'createFeatureResource');function getFeatures(){return getResourcesByName(resourceNames['feature']);}__name(getFeatures,'getFeatures');async function getOneFeatureByUserId(_0x1c5a1b,_0x419d5a){const _0x178a4b=await fetchFeatureResults(_0x1c5a1b,_0x419d5a);return processFeatureResults(_0x178a4b);}__name(getOneFeatureByUserId,'getOneFeatureByUserId');async function getFeatureByFeatureCode(_0x2c05ff){const _0x2867ac=getDbSchema(),_0x2b40d9=_0x2867ac[schemaNames['featureSchema']],{query:_0xdd2e39}=await getQuery(_0x2b40d9);applyFilterToQuery(_0xdd2e39,eq(getTableColumn(_0x2b40d9,columnNames['code']),_0x2c05ff));const _0x34287c=await runQuery(_0xdd2e39);return singleOrUndefined(_0x34287c);}__name(getFeatureByFeatureCode,'getFeatureByFeatureCode');function getFeatureODataConfig(_0x3ef5c7){const _0x375a9b=getODataBuilder();return _0x375a9b['filter'](featureProps['code'],_0x3ef5c7,oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal']}),_0x375a9b['filter'](featureProps['state'],featureStates['enabled'],oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal']}),_0x375a9b['expandMany']([featureProps['paymentPlanFeatureList'],featureProps['userFeatureList'],featureProps['roleFeatureList']]),_0x375a9b['build']();}__name(getFeatureODataConfig,'getFeatureODataConfig');function getUserRoleODataConfig(_0x12358e){const _0x43d844=getODataBuilder();return _0x43d844['filter'](userRoleProps['userId'],_0x12358e,oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal']}),_0x43d844['build']();}__name(getUserRoleODataConfig,'getUserRoleODataConfig');function getSubscriptionODataConfig(_0x2fb1dc){const _0x1a85b2=getODataBuilder();return _0x1a85b2['expand'](subscriptionProps['pricing']),_0x1a85b2['filter'](subscriptionProps['status'],subscriptionStatuses['active'],oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal']}),_0x1a85b2['filter'](subscriptionProps['status'],subscriptionStatuses['trialing'],oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal'],'logicalOperationType':oDataLogicalOperationTypes['or']}),_0x1a85b2['filter'](withWorkspaceIdProps['workspaceId'],_0x2fb1dc,oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal']}),_0x1a85b2['build']();}__name(getSubscriptionODataConfig,'getSubscriptionODataConfig');async function fetchFeatureResults(_0x18ba66,_0x5ee019){const _0xd68247=await getUserHomeWorkspaceId(_0x18ba66),[_0x5f217b,_0x10de40,_0x226e4e]=await Promise['all']([getResources(resourceNames['feature'],_0x18ba66,getFeatureODataConfig(_0x5ee019)),getResources(resourceNames['userRole'],_0x18ba66,getUserRoleODataConfig(_0x18ba66)),getResources(resourceNames['subscription'],_0x18ba66,getSubscriptionODataConfig(_0xd68247))]),_0x13fc2c=singleOrUndefined(_0x5f217b);if(!_0x13fc2c)return _0x5f217b;const _0x1026d6=singleOrUndefined(_0x10de40),_0x526370=singleOrUndefined(_0x226e4e);let _0x38a553=!!_0x13fc2c['userFeatureList']?.['find'](_0x15c05e=>_0x15c05e['userId']===_0x18ba66&&_0x15c05e['state']===userFeatureStates['enabled']);return !_0x38a553&&_0x1026d6&&(_0x38a553=!!_0x13fc2c['roleFeatureList']?.['find'](_0x3327fd=>_0x3327fd['roleId']===_0x1026d6['roleId']&&_0x3327fd['state']===roleFeatureStates['enabled'])),!_0x38a553&&_0x526370&&(_0x38a553=!!_0x13fc2c['paymentPlanFeatureList']?.['find'](_0x28d950=>_0x28d950['paymentPlanId']===_0x526370['pricing']?.['paymentPlanId']&&_0x28d950['state']===paymentPlanFeatureStates['enabled'])),_0x38a553?_0x5f217b:[];}__name(fetchFeatureResults,'fetchFeatureResults');function processFeatureResults(_0x6a4377){return firstOrUndefined(_0x6a4377);}__name(processFeatureResults,'processFeatureResults');

export { createFeatureResource, getFeatureByFeatureCode, getFeatures, getOneFeature, getOneFeatureByUserId };
