'use strict';
import { permissionTypes, permissionActions } from '../../core/resources/enums/permission-enums.js';
import { getCurrentWorkspaceId } from '../providers/current-workspace-id-provider.js';
import { crudActionNames } from '../../../../fd-toolbox/server/constants/crud-actions-constants.js';
import { generateDisplayName } from '../meta-core/display-name.js';
import { getResourceMeta } from '../services/metas.js';
import { isResourceSingleton } from '../../core/resource-singletons.js';
import { crudActions } from '../../../../fd-toolbox/enums/enums.js';
import { getBooleanEnvSetting } from '../../../../fd-toolbox/infra/env-functions.js';
import { toolboxEnvSettingKeys } from '../../../../fd-toolbox/infra/toolbox-env-setting-keys.js';
import { clearPermissionMemoryCache } from '../../../../fd-toolbox/server/cachings/fd-memory-cache.js';
import { clearPermissionRemoteCache } from '../../../../fd-toolbox/server/cachings/fd-remote-cache.js';
import { fdServerCacheKeys } from '../../../../fd-toolbox/server/constants/fd-server-cache-keys.js';
import { getPermission } from './permission-data-provider.js';
import { clearPendingPermissionsRequests } from './permission-checker.js';

var __defProp=Object['defineProperty'],__name=(_0x3760ee,_0x167f6e)=>__defProp(_0x3760ee,'name',{'value':_0x167f6e,'configurable':!![]});async function createPermissionDto(_0x3a9e47,_0x5c8c0a){const _0x2ec7cf={'id':_0x3a9e47['id'],'name':_0x3a9e47['name'],'level':_0x5c8c0a,'action':_0x3a9e47['action'],'resourceId':_0x3a9e47['resourceId'],'resourceName':_0x3a9e47['resource']?.['name'],'type':_0x3a9e47['type']??permissionTypes['default'],'resourcePropertyId':_0x3a9e47['resourcePropertyId']};if(_0x2ec7cf['resourceName']){_0x2ec7cf['displayName']=await generateDisplayName(_0x2ec7cf['resourceName']);const _0x1d12ea=await getResourceMeta(_0x2ec7cf['resourceName']);_0x2ec7cf['isSingleton']=isResourceSingleton(_0x1d12ea);}return _0x2ec7cf;}__name(createPermissionDto,'createPermissionDto');async function getPermissionCacheKey(_0x2cc0bb,_0xcb7c80){const _0x1fb9f1=await getCurrentWorkspaceId();return _0x2cc0bb+'_'+_0x1fb9f1+'_'+_0xcb7c80;}__name(getPermissionCacheKey,'getPermissionCacheKey');function getPermissionNameFromCrud(_0x525595,_0xbcf291,_0x543c70){const _0x2c22fe=getCrudActionStrings()['get'](_0xbcf291);if(_0x543c70)return _0x2c22fe+'_Property_'+_0x525595+'_'+_0x543c70;return _0x2c22fe+'_'+_0x525595;}__name(getPermissionNameFromCrud,'getPermissionNameFromCrud');function convertCrudActionToPermissionAction(_0x57a5e2){const _0x2aa369=[];return hasFlag(_0x57a5e2,permissionActions['create'])&&_0x2aa369['push'](crudActions['create']),hasFlag(_0x57a5e2,permissionActions['delete'])&&_0x2aa369['push'](crudActions['delete']),hasFlag(_0x57a5e2,permissionActions['update'])&&_0x2aa369['push'](crudActions['update']),hasFlag(_0x57a5e2,permissionActions['view'])&&_0x2aa369['push'](crudActions['view']),_0x2aa369;}__name(convertCrudActionToPermissionAction,'convertCrudActionToPermissionAction');function getCrudActionStrings(){const _0xfd4757=new Map([[crudActions['create'],crudActionNames['create']],[crudActions['view'],crudActionNames['view']],[crudActions['update'],crudActionNames['update']],[crudActions['delete'],crudActionNames['delete']]]);return _0xfd4757;}__name(getCrudActionStrings,'getCrudActionStrings');function createShallowPermission(_0x26dbee){return {..._0x26dbee};}__name(createShallowPermission,'createShallowPermission');async function removePermissionsFromCoreCache(){const _0xc2009e=[fdServerCacheKeys['roleToPackages'],fdServerCacheKeys['userRolePermissionLevels'],fdServerCacheKeys['userResourcePermissions'],fdServerCacheKeys['userDefaultPermissions'],fdServerCacheKeys['userPropertyPermissions'],fdServerCacheKeys['roleToResourcePermissions'],fdServerCacheKeys['roleToDefaultPermissions'],fdServerCacheKeys['roleToPropertyPermissions'],fdServerCacheKeys['packagesToResourcePermissions'],fdServerCacheKeys['packagesToDefaultPermissions'],fdServerCacheKeys['packagesToPropertyPermissions'],fdServerCacheKeys['permission']];getBooleanEnvSetting(toolboxEnvSettingKeys['useRemoteCache'])?await clearPermissionRemoteCache(_0xc2009e):clearPermissionMemoryCache(_0xc2009e),clearPendingPermissionsRequests();}__name(removePermissionsFromCoreCache,'removePermissionsFromCoreCache');async function addPermissions(_0x5d1f28,_0x2750b1){const _0x4b8549=[];for(const _0x298a3b of _0x2750b1){const _0x5f2a34=_0x5d1f28+':'+_0x298a3b,_0x1c7b62=await getPermission(_0x5f2a34);_0x1c7b62&&_0x4b8549['push'](_0x1c7b62);}return _0x4b8549;}__name(addPermissions,'addPermissions');function hasFlag(_0x536781,_0x427a79){return (_0x536781&_0x427a79)===_0x427a79;}__name(hasFlag,'hasFlag');

export { addPermissions, convertCrudActionToPermissionAction, createPermissionDto, createShallowPermission, getCrudActionStrings, getPermissionCacheKey, getPermissionNameFromCrud, removePermissionsFromCoreCache };
