'use strict';
import { permissionTypes } from '../../core/resources/enums/permission-enums.js';
import { getResourceMeta } from '../services/metas.js';
import { getResourcePermissionChecker } from '../auth/permissions/resource-permission-checker-provider.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { getPermissionCheckConfig } from '../services/permission-check-config-provider.js';
import { getUserPermissionCacheKey, getUserRoleIdsToRolePermissionLevels, getRoleToPackages, saveUserPermissionsToMemoryCache, saveRoleToPermissionsToCache, savePackageToPermissionsToCache, getPermission, getUserResourcePermissionCacheKey } from './permission-data-provider.js';
import { isValidUuid } from '../../../../fd-toolbox/uuids.js';
import { tryGetObjectFromCoreCache, setCoreCache } from '../../../../fd-toolbox/server/cachings/core-cache.js';
import { isNullOrUndefined } from '../../../../fd-toolbox/functions/value-checking-functions.js';
import { withUserIdProps } from '../../../../fd-toolbox/types/with-user-id.js';
import { convertCrudActionToPermissionAction, getPermissionNameFromCrud, createPermissionDto } from './permissions.js';
import { isPlainObject } from '../../../../fd-toolbox/lib/utils.js';
import { isString } from '../../../../fd-toolbox/types/ensure-type.js';
import { crudActions } from '../../../../fd-toolbox/enums/enums.js';

var __defProp=Object['defineProperty'],__name=(_0x26b6e2,_0x384c7d)=>__defProp(_0x26b6e2,'name',{'value':_0x384c7d,'configurable':!![]});const minPermissionLevel=0x19d+0x1790+0x509*-0x5;async function hasPermission(_0x8750f0,_0x1f7db6,_0x58adec){if(isString(_0x8750f0)){const _0x1e134b=await getResourceMeta(_0x8750f0),_0x4d5105=getPermissionCheckConfig(_0x58adec,_0x1e134b,void(-0x13*0x42+-0x2*-0x5f1+-0x6fc),_0x1f7db6);return hasPermissionByPermissionCheckConfig(_0x4d5105);}return hasPermissionByPermissionCheckConfig(_0x8750f0);}__name(hasPermission,'hasPermission');async function hasPropertyPermissionAsync(_0x427251,_0x1a4a29){let _0x3c3190=![];if(!_0x1a4a29['resourceMetaId']||!_0x1a4a29['crudAction'])return _0x3c3190;if(await checkUserPermission(_0x1a4a29))_0x3c3190=!![];else {const _0x4298cb=await getUserPermission(_0x1a4a29['userId'],permissionTypes['property'],_0x427251),_0x4e1bb8=!!(_0x4298cb?.['action']&&convertCrudActionToPermissionAction(_0x4298cb['action'])['includes'](_0x1a4a29['crudAction']));if(_0x4e1bb8)_0x3c3190=!![];else {if(_0x1a4a29['resourceName']&&_0x1a4a29['resource']){const _0x3c55bc=await getResourceMeta(_0x1a4a29['resourceName']);_0x3c55bc['auth']['accessByUserId']&&(_0x3c3190=checkBelowLevelAllowedCrudByUserId(_0x3c55bc,_0x1a4a29));}}}return _0x3c3190;}__name(hasPropertyPermissionAsync,'hasPropertyPermissionAsync');async function getUserPermissions(_0x33ba66){const [_0x39d8c9,_0xdba096,_0x14d70f]=await Promise['all']([getUserPermissionsByPermissionType(_0x33ba66,permissionTypes['resource']),getUserPermissionsByPermissionType(_0x33ba66,permissionTypes['property']),getUserPermissionsByPermissionType(_0x33ba66,permissionTypes['default'])]),_0x5df76d={'resourcePermissions':_0x39d8c9,'propertyPermissions':_0xdba096,'defaultPermissions':_0x14d70f};return _0x5df76d;}__name(getUserPermissions,'getUserPermissions');const pendingPermissionsRequests=new Map();function clearPendingPermissionsRequests(){pendingPermissionsRequests['clear']();}__name(clearPendingPermissionsRequests,'clearPendingPermissionsRequests');async function hasPermissionByPermissionCheckConfig(_0x5a3e48){_0x5a3e48['resourceName']&&!isNullOrUndefined(_0x5a3e48['crudAction'])&&(_0x5a3e48['permissionName']=getPermissionNameFromCrud(_0x5a3e48['resourceName'],_0x5a3e48['crudAction']));if(!_0x5a3e48['permissionName'])throw createError(_0x5a3e48['permissionName']+'\x20is\x20empty');const _0x3b2d70=await checkUserPermission(_0x5a3e48);if(_0x5a3e48['throwIfNoPermission']&&!_0x3b2d70)throw createError('No\x20permission\x20'+_0x5a3e48['permissionName']+'\x20for\x20'+_0x5a3e48['userId']);return _0x3b2d70;}__name(hasPermissionByPermissionCheckConfig,'hasPermissionByPermissionCheckConfig');async function checkUserPermission(_0x4e5f34){let _0x5bf2f9=await hasDefaultOrResourcePermission(_0x4e5f34);if(_0x4e5f34['resource']&&_0x4e5f34['resourceName']){const _0x2a36d7=await getResourceMeta(_0x4e5f34['resourceName']);!_0x5bf2f9&&(_0x5bf2f9=checkBelowLevelAllowedCrudByUserId(_0x2a36d7,_0x4e5f34));const _0xc02ceb=await getResourcePermissionChecker(_0x2a36d7['resourceName']);if(_0xc02ceb){const _0x103ff7=await _0xc02ceb(_0x4e5f34['resource'],_0x4e5f34['userId'],_0x4e5f34['crudAction']??crudActions['view']);_0x103ff7&&(_0x5bf2f9=_0x103ff7);}}return _0x5bf2f9;}__name(checkUserPermission,'checkUserPermission');async function getUserPermissionsByPermissionType(_0x1e07a2,_0x3b5985){const _0x2f1bcd=await getUserPermissionCacheKey(_0x3b5985,_0x1e07a2),_0x1262e2=await tryGetObjectFromCoreCache(_0x2f1bcd);if(_0x1262e2!==void(0x28*-0x6d+0x1a91+-0x989))return _0x1262e2;if(pendingPermissionsRequests['has'](_0x2f1bcd))return pendingPermissionsRequests['get'](_0x2f1bcd);const _0xf0e1c=((async()=>{try{const _0x2f88b5={},_0x2ca524={},_0x227208=await getUserRoleIdsToRolePermissionLevels(_0x1e07a2),_0x68e2cd=Object['keys'](_0x227208),_0x480d21=await getRoleToPackages(_0x68e2cd);await addRoleToPermissionsAndPackageToPermissions(_0x3b5985,_0x68e2cd,_0x480d21,_0x2f88b5,_0x2ca524);const _0xcea2a5={},_0x122092=Object['keys'](_0x227208)['length']>-0x1*0xb2b+0x22ce+0x17a3*-0x1?Math['max'](...Object['values'](_0x227208)):minPermissionLevel;return await addUserPermissions(_0x68e2cd,_0x2f88b5,_0x2ca524,_0x480d21,_0x122092,_0xcea2a5),await saveUserPermissionsToMemoryCache(_0x1e07a2,_0x3b5985,_0xcea2a5),_0xcea2a5;}finally{pendingPermissionsRequests['delete'](_0x2f1bcd);}})());return pendingPermissionsRequests['set'](_0x2f1bcd,_0xf0e1c),_0xf0e1c;}__name(getUserPermissionsByPermissionType,'getUserPermissionsByPermissionType');async function addRoleToPermissionsAndPackageToPermissions(_0x3bb6e0,_0x3f2ae3,_0x5d3414,_0x7e5e3b,_0x1b9fe6){await saveRoleToPermissionsToCache(_0x7e5e3b,_0x3f2ae3,_0x3bb6e0);const _0x8f4beb=[];for(const _0x241f6e of _0x3f2ae3){_0x241f6e in _0x5d3414&&_0x8f4beb['push'](..._0x5d3414[_0x241f6e]);}await savePackageToPermissionsToCache(_0x1b9fe6,_0x8f4beb,_0x3bb6e0);}__name(addRoleToPermissionsAndPackageToPermissions,'addRoleToPermissionsAndPackageToPermissions');function checkBelowLevelAllowedCrudByUserId(_0x53163b,_0x440dce){if(!_0x440dce['resource'])return ![];const _0x51c5d5=_0x440dce['resource'],_0x4f26af=withUserIdProps['userId']in _0x53163b['properties']&&isWithUserId(_0x51c5d5);return _0x4f26af&&_0x440dce['crudAction']!==void(0x779+0xb66+0x12df*-0x1)&&_0x53163b['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x440dce['crudAction'])&&_0x440dce['userId']===_0x51c5d5['userId'];}__name(checkBelowLevelAllowedCrudByUserId,'checkBelowLevelAllowedCrudByUserId');function isWithUserId(_0x3b11f9){return _0x3b11f9!==void(-0x78+0x4b6+-0x43e)&&isPlainObject(_0x3b11f9)&&withUserIdProps['userId']in _0x3b11f9&&isString(_0x3b11f9[withUserIdProps['userId']]);}__name(isWithUserId,'isWithUserId');async function hasDefaultOrResourcePermission(_0x4b1152){let _0x3ba307=![];if(_0x4b1152['resourceMetaId']){const _0x4253ce=await getUserPermission(_0x4b1152['userId'],permissionTypes['resource'],_0x4b1152['resourceMetaId']);_0x4253ce?.['action']&&_0x4b1152['crudAction']!==void(-0x1f*0x1d+0x21c1*0x1+-0x452*0x7)&&convertCrudActionToPermissionAction(_0x4253ce['action'])['includes'](_0x4b1152['crudAction'])&&(_0x3ba307=!![]);}else {if(_0x4b1152['permissionName']){const _0x1aadb7=await getUserPermission(_0x4b1152['userId'],permissionTypes['default'],_0x4b1152['permissionName']);_0x1aadb7&&_0x1aadb7['name']===_0x4b1152['permissionName']&&(_0x3ba307=!![]);}}return _0x3ba307;}__name(hasDefaultOrResourcePermission,'hasDefaultOrResourcePermission');async function addUserPermissions(_0x2b19ff,_0x3a5943,_0x1718b5,_0x2fb91f,_0x514817,_0x4ea89f){const _0x411133=collectAllPermissionIds(_0x2b19ff,_0x3a5943,_0x2fb91f,_0x1718b5),_0x124743=await Promise['all'](Array['from'](_0x411133)['map'](getPermission)),_0x1a0090=groupAndMergeFlags(_0x124743),_0x8ae41d=[];for(const [_0x202a00,_0x4e7e88]of _0x1a0090){_0x8ae41d['push'](((async()=>{_0x4ea89f[_0x202a00]=await createPermissionDto(_0x4e7e88,_0x514817);})()));}await Promise['all'](_0x8ae41d);}__name(addUserPermissions,'addUserPermissions');function collectAllPermissionIds(_0x429e82,_0x2df91a,_0x484e99,_0x14630f){const _0x4eaa71=new Set();for(const _0x2bd579 of _0x429e82){const _0x704a9c=_0x2df91a[_0x2bd579]??[];for(const _0xb8eacc of _0x704a9c){_0x4eaa71['add'](_0xb8eacc);}const _0x8b390=_0x484e99[_0x2bd579]??[];for(const _0x42cdd6 of _0x8b390){const _0x15bbe2=_0x14630f[_0x42cdd6]??[];for(const _0x37762a of _0x15bbe2){_0x4eaa71['add'](_0x37762a);}}}return _0x4eaa71;}__name(collectAllPermissionIds,'collectAllPermissionIds');function groupAndMergeFlags(_0x488d77){const _0x52e046=new Map();for(const _0x1f21a of _0x488d77){if(_0x1f21a){const _0x55ca6b=getPermissionKey(_0x1f21a);if(_0x55ca6b){const _0xb44e4e=_0x52e046['get'](_0x55ca6b);if(!_0xb44e4e)_0x52e046['set'](_0x55ca6b,_0x1f21a);else _0x1f21a['id']!==_0xb44e4e['id']&&_0x52e046['set'](_0x55ca6b,{..._0xb44e4e,'action':(_0xb44e4e['action']??0x34a+-0x38*-0x56+0x6*-0x3af)|(_0x1f21a['action']??0x15d8+0x25*-0xdb+0x9cf)});}}}return _0x52e046;}__name(groupAndMergeFlags,'groupAndMergeFlags');function getPermissionKey(_0x368f01){let _0x29632e;if(_0x368f01['type']===permissionTypes['resource']&&_0x368f01['resourceId'])_0x29632e=_0x368f01['resourceId'];else {if(_0x368f01['type']===permissionTypes['property']&&_0x368f01['resourcePropertyId'])_0x29632e=_0x368f01['resourcePropertyId'];else _0x368f01['type']===permissionTypes['default']&&_0x368f01['name']&&(_0x29632e=_0x368f01['name']);}return _0x29632e;}__name(getPermissionKey,'getPermissionKey');async function getUserPermission(_0x10d71c,_0x29c496,_0x12defc){const _0x17a522=await getUserResourcePermissionCacheKey(_0x29c496,_0x12defc,_0x10d71c);let _0x30005b=await tryGetObjectFromCoreCache(_0x17a522);if(_0x30005b!==void(-0x1*0x1f97+-0x256a+0x4501))return _0x30005b;const _0x482fcd=await getUserPermissionsByPermissionType(_0x10d71c,_0x29c496);_0x30005b=void(0xbaa*-0x2+-0x381+0x1*0x1ad5);if(_0x29c496===permissionTypes['resource']||_0x29c496===permissionTypes['property'])isValidUuid(_0x12defc)&&_0x12defc in _0x482fcd&&(_0x30005b=_0x482fcd[_0x12defc]);else {if(_0x29c496===permissionTypes['default']){const _0x393389=_0x12defc['toString']();_0x393389 in _0x482fcd&&(_0x30005b=_0x482fcd[_0x393389]);}}return _0x30005b&&await setCoreCache(_0x17a522,_0x30005b),_0x30005b;}__name(getUserPermission,'getUserPermission');

export { clearPendingPermissionsRequests, getUserPermissions, hasPermission, hasPropertyPermissionAsync };
