'use strict';
import { permissionTypes } from '../../core/resources/enums/permission-enums.js';
import { tryGetObjectFromCoreCache, setCoreCache } from '../../../../fd-toolbox/server/cachings/core-cache.js';
import { addListValueToDictionary } from '../../../../fd-toolbox/server/data/dictionaries.js';
import { getAllIncludingUserToRoles } from '../auth/role-db/get-all-including-user-to-roles.js';
import { getDbSchema } from '../../../db/db-schema.js';
import { getTableColumns, eq } from 'drizzle-orm';
import { executeQuery, getQuery } from '../web/drizzle.js';
import { runQuery } from '../../../db/drizzle-queries.js';
import { dbLeftJoin } from '../../../db/db-connection.js';
import { getPermissionPackageToPermissions } from './permission-package-to-permission-db.js';
import { getRoleToPermissionPackages } from '../models/roles/role-to-permission-package-db.js';
import { getPermissionCacheKey, createShallowPermission } from './permissions.js';
import { fdServerCacheKeys } from '../../../../fd-toolbox/server/constants/fd-server-cache-keys.js';
import { schemaNames, columnNames } from '../../../db/constants/schema-names.js';

var __defProp=Object['defineProperty'],__name=(_0x1debb9,_0xa9cbd2)=>__defProp(_0x1debb9,'name',{'value':_0xa9cbd2,'configurable':!![]});let pendingPermissionsSavePromise;async function getUserRoleIdsToRoleLevels(){const _0x2ef305=await getAllIncludingUserToRoles(),_0x44daec={};for(const _0x3420a4 of _0x2ef305){if(_0x3420a4['userRoleList'])for(const _0x27bbec of _0x3420a4['userRoleList']['filter'](_0x13ce2a=>_0x13ce2a['roleId']===_0x3420a4['id'])){!_0x44daec[_0x27bbec['userId']]&&(_0x44daec[_0x27bbec['userId']]={}),_0x44daec[_0x27bbec['userId']][_0x27bbec['roleId']]=_0x3420a4['level'];}}return _0x44daec;}__name(getUserRoleIdsToRoleLevels,'getUserRoleIdsToRoleLevels');async function saveRoleToPermissionsToCache(_0x3c9f55,_0x3fafe3,_0x4fa917){const _0x95ae1c=getRoleCacheKeyPrefix(_0x4fa917);for(const _0x2505c1 of _0x3fafe3){await addWithIndexerObjectFromCache(_0x3c9f55,_0x2505c1,_0x95ae1c),!(_0x2505c1 in _0x3c9f55)&&await loadRolePermissionsToCache(_0x3c9f55,_0x2505c1,_0x95ae1c);}return _0x3c9f55;}__name(saveRoleToPermissionsToCache,'saveRoleToPermissionsToCache');async function getRoleToPermissionList(){const _0x40dd41=getDbSchema(),_0x4577ec=await executeQuery(_0x40dd41[schemaNames['roleToPermissionSchema']]);return _0x4577ec;}__name(getRoleToPermissionList,'getRoleToPermissionList');async function getUserPermissionCacheKey(_0x4fb369,_0x480e33){if(_0x4fb369===permissionTypes['resource'])return getPermissionCacheKey(fdServerCacheKeys['userResourcePermissions'],_0x480e33);else return _0x4fb369===permissionTypes['property']?getPermissionCacheKey(fdServerCacheKeys['userPropertyPermissions'],_0x480e33):getPermissionCacheKey(fdServerCacheKeys['userDefaultPermissions'],_0x480e33);}__name(getUserPermissionCacheKey,'getUserPermissionCacheKey');async function getUserResourcePermissionCacheKey(_0x550249,_0x8e3cdd,_0x1aea34){const _0x23d344=await getUserPermissionCacheKey(_0x550249,_0x8e3cdd);return getPermissionCacheKey(_0x23d344,_0x1aea34);}__name(getUserResourcePermissionCacheKey,'getUserResourcePermissionCacheKey');async function saveUserPermissionsToMemoryCache(_0x4ffb64,_0x10e3ca,_0x2128d9){const _0x5dd203=await getUserPermissionCacheKey(_0x10e3ca,_0x4ffb64);await saveToCoreCache(_0x5dd203,_0x2128d9);}__name(saveUserPermissionsToMemoryCache,'saveUserPermissionsToMemoryCache');async function getUserRoleIdsToRolePermissionLevels(_0x171ac2){const _0x145749=await getPermissionCacheKey(fdServerCacheKeys['userRolePermissionLevels'],_0x171ac2);let _0x361711=await tryGetObjectFromCoreCache(_0x145749);if(_0x361711)return _0x361711;await saveUserRoleIdsToRolePermissionLevelsToCache(),_0x361711=await tryGetObjectFromCoreCache(_0x145749);if(!_0x361711)return await saveToCoreCache(_0x145749,{}),{};return _0x361711;}__name(getUserRoleIdsToRolePermissionLevels,'getUserRoleIdsToRolePermissionLevels');async function savePackageToPermissionsToCache(_0x52ab30,_0x3ebac9,_0x287616){const _0x30cdf6=getPackageCacheKeyPrefix(_0x287616);for(const _0x44fd5c of _0x3ebac9){await addWithIndexerObjectFromCache(_0x52ab30,_0x44fd5c,_0x30cdf6),!(_0x44fd5c in _0x52ab30)&&(await privateSavePackageToPermissionsToCache(_0x44fd5c),await loadFromCacheOrSetEmpty(_0x52ab30,_0x44fd5c,_0x30cdf6));}return _0x52ab30;}__name(savePackageToPermissionsToCache,'savePackageToPermissionsToCache');async function getPermission(_0x4e0179){const _0x1a4299=await getPermissionCacheKey(fdServerCacheKeys['permission'],_0x4e0179);let _0x425bae,_0x470747=await tryGetObjectFromCoreCache(_0x1a4299);_0x470747===void(-0x1559+-0x23e4+0x393d*0x1)&&(pendingPermissionsSavePromise??=savePermissionsToCache()['finally'](()=>{pendingPermissionsSavePromise=void(-0x2*-0x582+-0x220c+0x1708);}),await pendingPermissionsSavePromise,_0x470747=await tryGetObjectFromCoreCache(_0x1a4299));if(!_0x470747)await saveToCoreCache(_0x1a4299,{'isInvalid':!![]});else !_0x470747['isInvalid']&&(_0x425bae=createShallowPermission(_0x470747));return _0x425bae;}__name(getPermission,'getPermission');async function addPackageToPermissions(_0x4af81a,_0x131d11,_0x23ccbf){const _0xa207bb=[];for(const _0x1f75bb of _0x131d11){_0xa207bb['push'](((async()=>{const _0x1d6bb3=await getPermission(_0x1f75bb['permissionId']);_0x1d6bb3?.['type']===_0x23ccbf&&addListValueToDictionary(_0x4af81a,_0x1f75bb['permissionPackageId'],_0x1f75bb['permissionId']);})()));}await Promise['all'](_0xa207bb);}__name(addPackageToPermissions,'addPackageToPermissions');async function mapRawPermissionToPermission(){const _0x413d7d=getDbSchema(),_0x1c0451=_0x413d7d[schemaNames['permissionSchema']],_0x5d5318=_0x413d7d[schemaNames['resourceSchema']],_0x471116=getTableColumns(_0x1c0451),_0x118f6b=getTableColumns(_0x5d5318),{query:_0x399589}=await getQuery(_0x1c0451,{..._0x471116,'resource':_0x118f6b},!![]);dbLeftJoin(_0x399589,_0x5d5318,eq(_0x471116[columnNames['resourceId']],_0x118f6b[columnNames['id']]));const _0xec1e18=await runQuery(_0x399589),_0x4d505b=[];for(const _0x3e6541 of _0xec1e18){_0x4d505b['push']({'id':_0x3e6541['id'],'name':_0x3e6541['name']??void(0xcba+-0x17*0xa1+-0x59*-0x5),'type':_0x3e6541['type'],'action':_0x3e6541['action']??void(-0x2d*0x7c+0x1c51+-0x685),'resourceId':_0x3e6541['resourceId']??void(-0x12c*-0x6+0x9*-0x3b5+0x2ed*0x9),'resourcePropertyId':_0x3e6541['resourcePropertyId']??void(-0xa67+0x819+-0x3b*-0xa),'resource':_0x3e6541['resource']??{'id':'','name':''}});}return _0x4d505b;}__name(mapRawPermissionToPermission,'mapRawPermissionToPermission');async function savePermissionsToCache(){const _0x955b5a=await mapRawPermissionToPermission(),_0x31cf30=_0x955b5a['map'](async _0x26f2f2=>{const _0x58f6e8=await getPermissionCacheKey(fdServerCacheKeys['permission'],_0x26f2f2['id']);return saveToCoreCache(_0x58f6e8,_0x26f2f2);});await Promise['all'](_0x31cf30);}__name(savePermissionsToCache,'savePermissionsToCache');async function getRoleToPackages(_0xfd132c){const _0x164960={};for(const _0x5b57d6 of _0xfd132c){await addWithIndexerObjectFromCache(_0x164960,_0x5b57d6,fdServerCacheKeys['roleToPackages']),!(_0x5b57d6 in _0x164960)&&(await saveRoleToPermissionPackagesToCache(),await loadFromCacheOrSetEmpty(_0x164960,_0x5b57d6,fdServerCacheKeys['roleToPackages']));}return _0x164960;}__name(getRoleToPackages,'getRoleToPackages');async function addWithIndexerObjectFromCache(_0xaf2f6b,_0x3e91a6,_0x248280){const _0x512d25=await getPermissionCacheKey(_0x248280,_0x3e91a6),_0x1203d0=await tryGetObjectFromCoreCache(_0x512d25);_0x1203d0&&(_0xaf2f6b[_0x3e91a6]=_0x1203d0);}__name(addWithIndexerObjectFromCache,'addWithIndexerObjectFromCache');async function saveRoleToPermissionsToCacheById(){const _0x4e3219=new Map(),_0x50c037=new Map(),_0x25f4e3=new Map(),_0x4b7e8a=await getRoleToPermissionList();await addRoleToPermissions(_0x4e3219,_0x4b7e8a,permissionTypes['resource']),await addRoleToPermissions(_0x50c037,_0x4b7e8a,permissionTypes['default']),await addRoleToPermissions(_0x25f4e3,_0x4b7e8a,permissionTypes['property']),await saveToMemoryCacheByCacheKeyPrefix(_0x4e3219,fdServerCacheKeys['roleToResourcePermissions']),await saveToMemoryCacheByCacheKeyPrefix(_0x50c037,fdServerCacheKeys['roleToDefaultPermissions']),await saveToMemoryCacheByCacheKeyPrefix(_0x25f4e3,fdServerCacheKeys['roleToPropertyPermissions']);}__name(saveRoleToPermissionsToCacheById,'saveRoleToPermissionsToCacheById');async function addRoleToPermissions(_0x5e7388,_0x27018a,_0x2f56b7){const _0x13738f=[];for(const _0x417f83 of _0x27018a){_0x13738f['push'](((async()=>{const _0xc0a56=await getPermission(_0x417f83['permissionId']);_0xc0a56?.['type']===_0x2f56b7&&addListValueToDictionary(_0x5e7388,_0x417f83['roleId'],_0x417f83['permissionId']);})()));}await Promise['all'](_0x13738f);}__name(addRoleToPermissions,'addRoleToPermissions');async function applyPermissionsToPackages(_0x32a6e3,_0x368530,_0x58329f,_0x6c17ce){await addPackageToPermissions(_0x32a6e3,_0x368530,_0x58329f);if(_0x32a6e3['size']===-0x2343+-0x24d5*-0x1+-0x192)for(const _0x21423a of _0x6c17ce){_0x32a6e3['set'](_0x21423a,[]);}}__name(applyPermissionsToPackages,'applyPermissionsToPackages');async function privateSavePackageToPermissionsToCache(_0x25e905){const _0x3f2890=new Map(),_0x235e25=new Map(),_0x38234c=new Map(),_0xeb9bb1=await getPermissionPackageToPermissions();let _0x57cf62=[_0x25e905];_0x57cf62=_0x57cf62['concat'](_0xeb9bb1['map'](_0x5d8bac=>_0x5d8bac['permissionPackageId'])),_0x57cf62=Array['from'](new Set(_0x57cf62)),await applyPermissionsToPackages(_0x3f2890,_0xeb9bb1,permissionTypes['resource'],_0x57cf62),await applyPermissionsToPackages(_0x235e25,_0xeb9bb1,permissionTypes['default'],_0x57cf62),await applyPermissionsToPackages(_0x38234c,_0xeb9bb1,permissionTypes['property'],_0x57cf62),await saveToMemoryCacheByCacheKeyPrefix(_0x3f2890,fdServerCacheKeys['packagesToResourcePermissions']),await saveToMemoryCacheByCacheKeyPrefix(_0x235e25,fdServerCacheKeys['packagesToDefaultPermissions']),await saveToMemoryCacheByCacheKeyPrefix(_0x38234c,fdServerCacheKeys['packagesToPropertyPermissions']);}__name(privateSavePackageToPermissionsToCache,'privateSavePackageToPermissionsToCache');async function saveRoleToPermissionPackagesToCache(){const _0x10f49a=new Map(),_0x5dfe26=await getRoleToPermissionPackages();for(const _0x487204 of _0x5dfe26){addListValueToDictionary(_0x10f49a,_0x487204['roleId'],_0x487204['permissionPackageId']);}await saveToMemoryCacheByCacheKeyPrefix(_0x10f49a,fdServerCacheKeys['roleToPackages']);}__name(saveRoleToPermissionPackagesToCache,'saveRoleToPermissionPackagesToCache');async function saveToMemoryCacheByCacheKeyPrefix(_0x53a794,_0x8c07fd){const _0x460928=[];for(const [_0x39ef05,_0x2ddceb]of _0x53a794){_0x460928['push'](((async()=>{const _0x17bdcf=await getPermissionCacheKey(_0x8c07fd,_0x39ef05);await saveToCoreCache(_0x17bdcf,_0x2ddceb);})()));}await Promise['all'](_0x460928);}__name(saveToMemoryCacheByCacheKeyPrefix,'saveToMemoryCacheByCacheKeyPrefix');async function saveUserRoleIdsToRolePermissionLevelsToCache(){const _0x3a6548=await getUserRoleIdsToRoleLevels();for(const [_0x30105b,_0x324146]of Object['entries'](_0x3a6548)){const _0x568ed5=await getPermissionCacheKey(fdServerCacheKeys['userRolePermissionLevels'],_0x30105b);await saveToCoreCache(_0x568ed5,_0x324146);}}__name(saveUserRoleIdsToRolePermissionLevelsToCache,'saveUserRoleIdsToRolePermissionLevelsToCache');async function saveToCoreCache(_0xed0fb9,_0x5eb6f5){const _0xb81f8f={'absoluteExpirationRelativeToNowSeconds':(-0xbbd+0xe45*0x1+-0x270)*fdServerCacheKeys['secondsInAHour']};await setCoreCache(_0xed0fb9,_0x5eb6f5,_0xb81f8f);}__name(saveToCoreCache,'saveToCoreCache');function getRoleCacheKeyPrefix(_0x26dff7){let _0x5fb888=fdServerCacheKeys['roleToDefaultPermissions'];if(_0x26dff7===permissionTypes['resource'])_0x5fb888=fdServerCacheKeys['roleToResourcePermissions'];else _0x26dff7===permissionTypes['property']&&(_0x5fb888=fdServerCacheKeys['roleToPropertyPermissions']);return _0x5fb888;}__name(getRoleCacheKeyPrefix,'getRoleCacheKeyPrefix');function getPackageCacheKeyPrefix(_0x4db3bf){let _0x2317dd=fdServerCacheKeys['packagesToDefaultPermissions'];if(_0x4db3bf===permissionTypes['resource'])_0x2317dd=fdServerCacheKeys['packagesToResourcePermissions'];else _0x4db3bf===permissionTypes['property']&&(_0x2317dd=fdServerCacheKeys['packagesToPropertyPermissions']);return _0x2317dd;}__name(getPackageCacheKeyPrefix,'getPackageCacheKeyPrefix');async function loadRolePermissionsToCache(_0x350122,_0xbbeb7d,_0x3a6b7d){await saveRoleToPermissionsToCacheById(),await addWithIndexerObjectFromCache(_0x350122,_0xbbeb7d,_0x3a6b7d);if(_0xbbeb7d in _0x350122)return;await saveToCoreCache(await getPermissionCacheKey(_0x3a6b7d,_0xbbeb7d),[]),_0x350122[_0xbbeb7d]=[];}__name(loadRolePermissionsToCache,'loadRolePermissionsToCache');async function loadFromCacheOrSetEmpty(_0x36ed42,_0x21c577,_0x49f056){await addWithIndexerObjectFromCache(_0x36ed42,_0x21c577,_0x49f056);if(_0x21c577 in _0x36ed42)return;await saveToCoreCache(await getPermissionCacheKey(_0x49f056,_0x21c577),[]),_0x36ed42[_0x21c577]=[];}__name(loadFromCacheOrSetEmpty,'loadFromCacheOrSetEmpty');

export { addPackageToPermissions, getPermission, getRoleToPackages, getRoleToPermissionList, getUserPermissionCacheKey, getUserResourcePermissionCacheKey, getUserRoleIdsToRoleLevels, getUserRoleIdsToRolePermissionLevels, mapRawPermissionToPermission, savePackageToPermissionsToCache, savePermissionsToCache, saveRoleToPermissionsToCache, saveUserPermissionsToMemoryCache };
