'use strict';
import { getResourcesByName } from '../../meta/server/db/db.js';
import { getQuery, executeQuery } from '../../meta/server/web/drizzle.js';
import { getTableColumn, applyFilterToQuery, runQuery } from '../../db/drizzle-queries.js';
import { eq, getTableColumns, and } from 'drizzle-orm';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { createBaseResourceProperties } from '../utils/base-resource-properties.js';
import { workspaceConstants } from '../../../fd-toolbox/server/constants/workspace-constants.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../meta/constants/meta-error-messages-constants.js';
import { getUserFeatureByFeatureCode } from './user-feature-predicates.js';
import { userFeatureStates } from '../../meta/core/resources/enums/feature-enums.js';
import { createResource } from '../../meta/server/resources/crud-resources.js';
import { dbLeftJoin } from '../../db/db-connection.js';
import { getDbSchema } from '../../db/db-schema.js';
import { schemaNames, columnNames } from '../../db/constants/schema-names.js';
import { singleOrUndefined } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x2e7afd,_0x31d394)=>__defProp(_0x2e7afd,'name',{'value':_0x31d394,'configurable':!![]});async function getOneUserFeatureEnabledByUserId(_0x12cc93,_0x29c3cf){const _0x50aba6=getDbSchema(),_0x544443=_0x50aba6[schemaNames['featureSchema']],_0x1b5965=_0x50aba6[schemaNames['userFeatureSchema']],{query:_0x54b0d2}=await getQuery(_0x1b5965);dbLeftJoin(_0x54b0d2,_0x544443,eq(getTableColumn(_0x544443,columnNames['id']),getTableColumn(_0x1b5965,columnNames['featureId']))),applyFilterToQuery(_0x54b0d2,getUserFeatureByFeatureCode(_0x12cc93,_0x29c3cf)),applyFilterToQuery(_0x54b0d2,eq(getTableColumn(_0x1b5965,columnNames['userId']),_0x29c3cf));const _0x18547f=await runQuery(_0x54b0d2);return singleOrUndefined(_0x18547f);}__name(getOneUserFeatureEnabledByUserId,'getOneUserFeatureEnabledByUserId');async function getUserFeatures(){return getResourcesByName(resourceNames['userFeature']);}__name(getUserFeatures,'getUserFeatures');async function getUserFeaturesByUserId(_0x334915){const _0xbb5387=getDbSchema(),_0x4047f7=_0xbb5387[schemaNames['userFeatureSchema']],{query:_0x2a3e61}=await getQuery(_0x4047f7);applyFilterToQuery(_0x2a3e61,eq(getTableColumn(_0x4047f7,columnNames['userId']),_0x334915));const _0x5324ec=await runQuery(_0x2a3e61);return _0x5324ec;}__name(getUserFeaturesByUserId,'getUserFeaturesByUserId');async function tryGetOneByCode(_0x296137,_0x16405d){const _0x4cb0e3=getDbSchema(),_0x35bb0e=_0x4cb0e3[schemaNames['featureSchema']],_0x504e41=_0x4cb0e3[schemaNames['userFeatureSchema']],{query:_0x483428}=await getQuery(_0x504e41,getTableColumns(_0x504e41));dbLeftJoin(_0x483428,_0x35bb0e,eq(getTableColumn(_0x35bb0e,columnNames['id']),getTableColumn(_0x504e41,columnNames['featureId']))),applyFilterToQuery(_0x483428,and(eq(getTableColumn(_0x504e41,columnNames['userId']),_0x296137),eq(getTableColumn(_0x35bb0e,columnNames['code']),_0x16405d)));const _0x37de51=await runQuery(_0x483428);return singleOrUndefined(_0x37de51);}__name(tryGetOneByCode,'tryGetOneByCode');async function createUserFeatures(_0x8325db,_0x2d108d,_0x473d38){const _0x5f3368=new Map([]),_0x54cf1d=Array['from'](_0x2d108d['keys']());for(const _0x177c59 of _0x54cf1d){!_0x5f3368['has'](_0x177c59)&&_0x5f3368['set'](_0x177c59,![]);}for(const [_0x1d76c3,_0x2d4026]of _0x5f3368){const _0x2622b1=_0x2d4026?userFeatureStates['enabled']:userFeatureStates['disabled'];await createUserFeature(_0x8325db,_0x1d76c3,_0x2d108d,_0x473d38,_0x2622b1);}}__name(createUserFeatures,'createUserFeatures');async function getUserFeatureToUserIdMap(){const _0x28d881=getDbSchema(),_0xb58953=_0x28d881[schemaNames['userFeatureSchema']],_0x2af2ea=new Map(),_0x44ac46=await executeQuery(_0xb58953);for(const _0x5ee56c of _0x44ac46){const _0x487406=_0x5ee56c['userId'];!_0x2af2ea['has'](_0x487406)&&_0x2af2ea['set'](_0x487406,[]),_0x2af2ea['get'](_0x487406)['push'](_0x5ee56c);}return _0x2af2ea;}__name(getUserFeatureToUserIdMap,'getUserFeatureToUserIdMap');async function createUserFeature(_0x153d6a,_0x451ab9,_0x1a205f,_0x21a1a6,_0x62a79d){if(!_0x1a205f['has'](_0x451ab9))throw createError(metaErrorMessages['featureCodeMissing']['replace']('{0}',_0x451ab9));const _0x2db350=_0x1a205f['get'](_0x451ab9),_0x3c0d55=_0x21a1a6['find'](_0x1a8320=>_0x1a8320['featureId']===_0x2db350['id']&&_0x1a8320['userId']===_0x153d6a);if(!_0x3c0d55){const _0x4deedd={...createBaseResourceProperties(),'featureId':_0x2db350['id'],'state':_0x62a79d,'userId':_0x153d6a,'workspaceId':workspaceConstants['defaultWorkspaceId']};await createResource(_0x4deedd,resourceNames['userFeature']);}}__name(createUserFeature,'createUserFeature');

export { createUserFeatures, getOneUserFeatureEnabledByUserId, getUserFeatureToUserIdMap, getUserFeatures, getUserFeaturesByUserId, tryGetOneByCode };
