'use strict';
import { permissionTypes, permissionActions } from '../../../../meta/core/resources/enums/permission-enums.js';
import { getNewUuid } from '../../../../../fd-toolbox/uuids.js';
import { convertCrudActionToPermissionAction } from '../../../../meta/server/permission/permissions.js';
import { addMapValueToMap } from '../../../../../fd-toolbox/server/data/dictionaries.js';
import { getPermissions, createPermissionDirectlyInDb, updatePermission } from '../auth/permissions/permission-db.js';
import { createPermissionPackage } from '../auth/permissions/permission-package-db.js';
import { getListByPermissionPackageId } from '../auth/permissions/permission-package-to-db.js';
import { addPermissionPackageToPermissionDirectlyInDb } from '../../../../meta/server/permission/permission-package-to-permission-db.js';
import { resourceNames } from '../../../../../fd-toolbox/resources/resource-names.js';
import { findResourceById } from '../../../../meta/server/db/db.js';
import { crudActions } from '../../../../../fd-toolbox/enums/enums.js';

var __defProp=Object['defineProperty'],__name=(_0x340c9b,_0x2d3220)=>__defProp(_0x340c9b,'name',{'value':_0x2d3220,'configurable':!![]});async function configureResourcePermissions(_0x230a0f,_0x4e2dce,_0x30d91c){const _0x2f90c6=await getPermissions();for(const _0x28f371 of _0x230a0f){for(const _0x17610b of getAllPermissionActionList()){let _0x21fe4c=_0x2f90c6['find'](_0x41d338=>_0x41d338['resourceId']===_0x28f371&&_0x41d338['action']===_0x17610b);if(!_0x21fe4c){const _0x46be1={'id':getNewUuid(),'type':permissionTypes['resource'],'resourceId':_0x28f371,'action':_0x17610b,'workspaceId':_0x30d91c??void(-0xa66*-0x1+-0x238b+-0x9d*-0x29)};_0x21fe4c=await createPermissionDirectlyInDb(_0x46be1);}addMapValueToMap(_0x4e2dce,_0x28f371['toString'](),_0x17610b['toString'](),_0x21fe4c);}}}__name(configureResourcePermissions,'configureResourcePermissions');async function seedPermissionPackageToPermissionIfNotExist(_0x401e0d,_0xf2e34e){const _0x48758e=await getListByPermissionPackageId(_0x401e0d),_0x3348db=_0x48758e['map'](_0x45ecaf=>_0x45ecaf['permissionId']);for(const _0x149e69 of _0xf2e34e){!_0x3348db['includes'](_0x149e69)&&await configurePermissionPackageToPermission(_0x401e0d,_0x149e69);}}__name(seedPermissionPackageToPermissionIfNotExist,'seedPermissionPackageToPermissionIfNotExist');async function createAndSeedPermissionPackage(_0x41d07e,_0x52d3a3){const _0x37c8e7={'id':getNewUuid(),'name':'','permissionPackageToPermissionList':[]};_0x37c8e7['id']=_0x41d07e,_0x37c8e7['name']=_0x52d3a3,createPermissionPackage(_0x37c8e7);}__name(createAndSeedPermissionPackage,'createAndSeedPermissionPackage');function getAllPermissionActions(){return permissionActions['create']|permissionActions['delete']|permissionActions['update']|permissionActions['view'];}__name(getAllPermissionActions,'getAllPermissionActions');function getAllPermissionActionList(){return [permissionActions['delete'],permissionActions['update'],permissionActions['create'],permissionActions['view']];}__name(getAllPermissionActionList,'getAllPermissionActionList');function addPermissionIds(_0x51c521,_0xaca3df,_0x79bc80,_0x9a5d6a){const _0x4ec5fb=convertCrudActionToPermissionAction(_0xaca3df);for(const _0x14bc4b of _0x51c521){const _0x250583=_0x9a5d6a['get'](_0x14bc4b);for(const _0x534dba of _0x4ec5fb){let _0x24898f=_0x250583['get'](localConvertCrudAction(_0x534dba)['toString']());if(_0x24898f)_0x79bc80['push'](_0x24898f['id']);}}}__name(addPermissionIds,'addPermissionIds');async function configurePermission(_0x17743c,_0x40890f,_0x295356){const _0x42e756=await findResourceById(resourceNames['permission'],_0x17743c);if(!_0x42e756){const _0x538c7e={'id':_0x17743c,'type':permissionTypes['default'],'name':_0x40890f,'workspaceId':_0x295356};await createPermissionDirectlyInDb(_0x538c7e);}else _0x42e756['name']!==_0x40890f&&(_0x42e756['name']=_0x40890f,await updatePermission(_0x42e756));}__name(configurePermission,'configurePermission');async function configurePermissionPackageToPermission(_0x3cbd19,_0x5819f8){const _0x3bfdd6={'id':getNewUuid(),'isForbidden':![],'permissionPackageId':_0x3cbd19,'permissionId':_0x5819f8};await addPermissionPackageToPermissionDirectlyInDb(_0x3bfdd6);}__name(configurePermissionPackageToPermission,'configurePermissionPackageToPermission');function addPropertyPermissions(_0x473c4d,_0xbb8a8,_0x2bcd5a,_0x40aca7,_0x5881a8){const _0x3d0048=convertCrudActionToPermissionAction(_0x40aca7),_0x484591=_0x5881a8['get'](_0x473c4d);for(const _0x24eb5c of _0xbb8a8){const _0x2bc6c8=_0x484591['get'](_0x24eb5c);for(const _0x5ddb0a of _0x3d0048){const _0xc9c6f5=_0x2bc6c8['get'](_0x5ddb0a['toString']());_0x2bcd5a['push'](_0xc9c6f5['id']);}}}__name(addPropertyPermissions,'addPropertyPermissions');function localConvertCrudAction(_0x9aeb01){switch(_0x9aeb01){case crudActions['create']:return permissionActions['create'];case crudActions['delete']:return permissionActions['delete'];case crudActions['view']:return permissionActions['view'];case crudActions['update']:return permissionActions['update'];default:return permissionActions['view'];}}__name(localConvertCrudAction,'localConvertCrudAction');

export { addPermissionIds, addPropertyPermissions, configurePermission, configurePermissionPackageToPermission, configureResourcePermissions, createAndSeedPermissionPackage, getAllPermissionActionList, getAllPermissionActions, seedPermissionPackageToPermissionIfNotExist };
