'use strict';
import { permissionActions, permissionTypes } from '../../meta/core/resources/enums/permission-enums.js';
import { permissionPackages, permissions } from '../../common/auth/permissions/permission-constants.js';
import { resourceIds } from '../../../fd-toolbox/constants/resource-id-constants.js';
import { getPermissionPackage, updatePermissionPackage } from '../fd/fd-toolbox-web/auth/permissions/permission-package-db.js';
import { configureResourcePermissions, configurePermission, addPermissionIds, getAllPermissionActions, seedPermissionPackageToPermissionIfNotExist } from '../fd/fd-toolbox-web/permissions/fd-permission-master-data.js';
import { getResourceIds } from '../../meta/server/meta-core/constants/resource-ids.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { getResourceMeta } from '../../meta/server/services/metas.js';
import { getOData } from '../../meta/server/repositories/odata-db.js';
import { createBaseResourceProperties } from '../utils/base-resource-properties.js';
import { oDataOperators } from '../../../fd-toolbox/enums/enums.js';
import { ODataBuilder } from '../../../fd-toolbox/odata/odata-builders.js';
import { oDataComparisonTypes } from '../../../fd-toolbox/odata/odata-enums.js';
import { convertCrudActionToPermissionAction } from '../../meta/server/permission/permissions.js';
import { getAllResourceIds, addCommonPermissions } from './permission-role-mapping.js';
import { seedDevPermissions, seedDemoPermissions } from './dev-demo-permissions.js';
import { roleIds } from '../../common/auth/roles/role-ids.js';
import { getRoleToPermissionPackages, createRoleToPermissionPackageDirectlyInDb } from '../../meta/server/models/roles/role-to-permission-package-db.js';
import { seedAdminRoleToPermission } from './role-to-permission-master-data.js';
import { resourcePropertyProps } from '../../meta/core/resources/resources/props/resource-property-props.js';
import { createResourceDirectlyInDb } from '../../meta/server/db/db.js';
import { createPermissionDirectlyInDb } from '../fd/fd-toolbox-web/auth/permissions/permission-db.js';
import { v4 } from 'uuid';
import { getNow } from '../../../fd-toolbox/time/date-time.js';
import { toolboxUserConstants } from '../../meta/constants/toolbox-users.js';

var __defProp=Object['defineProperty'],__name=(_0xf06396,_0x24425e)=>__defProp(_0xf06396,'name',{'value':_0x24425e,'configurable':!![]});async function seedPermissionsDirectlyInDb(_0x46fa30){const _0x403953=new Map(),_0x278a53=new Map();await seedPermissionPackages(_0x46fa30),await seedResourcePermissions(_0x403953,_0x46fa30),await createAndSeedPermissions(_0x46fa30),await createAndSeedPropertyPermissions(_0x278a53,_0x46fa30),await createAndSeedRoleToPackageIfNotExist(),await createAndSeedRoleToPermission(),await seedGeneralPermissionPackageToPermission(_0x403953),await seedDevPermissions(_0x403953),await seedDemoPermissions(_0x403953),await seedAdminPermissionPackageToPermission(_0x403953),await seedSuperAdminPermissionPackageToPermission(_0x403953),await seedWorkspaceAdminPermissionPackageToPermission(_0x403953);}__name(seedPermissionsDirectlyInDb,'seedPermissionsDirectlyInDb');async function createAndSeedRoleToPermission(){await seedAdminRoleToPermission();}__name(createAndSeedRoleToPermission,'createAndSeedRoleToPermission');async function seedPermissionPackages(_0x241518){const _0x69ab4c=new Map([[permissionPackages['generalId'],permissionPackages['general']],[permissionPackages['adminId'],permissionPackages['admin']],[permissionPackages['devId'],permissionPackages['dev']],[permissionPackages['superAdminId'],permissionPackages['superAdmin']],[permissionPackages['workspaceAdminId'],permissionPackages['workspaceAdmin']]]);for(const [_0x4c3109,_0x3b3d18]of _0x69ab4c){const _0x5b6044=await getPermissionPackage(_0x4c3109);if(!_0x5b6044)await createResourceDirectlyInDb({'id':_0x4c3109,'name':_0x3b3d18,'workspaceId':_0x241518},resourceNames['permissionPackage']);else _0x5b6044['name']!==_0x3b3d18&&await updatePermissionPackage({'id':_0x4c3109,'name':_0x3b3d18,'workspaceId':_0x241518});}}__name(seedPermissionPackages,'seedPermissionPackages');async function seedResourcePermissions(_0x2c0ddb,_0xd10267){const _0xd17cbb=await getAllResourceIds();await configureResourcePermissions(_0xd17cbb,_0x2c0ddb,_0xd10267);}__name(seedResourcePermissions,'seedResourcePermissions');async function createAndSeedPermissions(_0x206c9e){const _0x30d5fa=new Map([[permissions['editOtherUserId'],permissions['editOtherUser']],[permissions['manageSubscriptionsId'],permissions['manageSubscriptions']],[permissions['approveProductId'],permissions['approveProduct']]]);for(const [_0x21dd3e,_0x5f53d5]of _0x30d5fa){await configurePermission(_0x21dd3e,_0x5f53d5,_0x206c9e);}}__name(createAndSeedPermissions,'createAndSeedPermissions');async function seedAdminPermissionPackageToPermission(_0x598ce7){const _0xc3ded0=[],_0xde0195=await getResourceIds(),_0x2d2a3c=_0xde0195['filter'](_0x4b6779=>_0x4b6779!==resourceIds['userSetting']&&_0x4b6779!==resourceIds['workspaceSetting']);addSettingsPermissions(_0xc3ded0,_0x598ce7),addPermissionIds([resourceIds['userSetting']],permissionActions['create']|permissionActions['update'],_0xc3ded0,_0x598ce7),addPermissionIds(_0x2d2a3c,getAllPermissionActions(),_0xc3ded0,_0x598ce7),await seedPermissionPackageToPermissionIfNotExist(permissionPackages['adminId'],_0xc3ded0);}__name(seedAdminPermissionPackageToPermission,'seedAdminPermissionPackageToPermission');async function seedCustomerPermissionPackageToPermission(_0x5b42f0){const _0x65f2fa=[resourceIds['user'],resourceIds['order']];await seedCommonSubscriptionPermissions(permissionPackages['userId'],_0x5b42f0,_0x65f2fa);}__name(seedCustomerPermissionPackageToPermission,'seedCustomerPermissionPackageToPermission');async function seedGeneralPermissionPackageToPermission(_0x5131af){const _0x54b973=[];await seedCommonSubscriptionPermissions(permissionPackages['generalId'],_0x5131af,[],[],_0x54b973);}__name(seedGeneralPermissionPackageToPermission,'seedGeneralPermissionPackageToPermission');async function seedWorkspaceAdminPermissionPackageToPermission(_0x2249d2){const _0x20a591=[resourceIds['user'],resourceIds['role'],resourceIds['userRole']],_0x493683=[];addPermissionIds([resourceIds['userSetting']],permissionActions['view'],_0x493683,_0x2249d2),await seedCommonSubscriptionPermissions(permissionPackages['workspaceAdminId'],_0x2249d2,void(0x62*0x6+-0x1c1e+0x19d2),_0x20a591,_0x493683);}__name(seedWorkspaceAdminPermissionPackageToPermission,'seedWorkspaceAdminPermissionPackageToPermission');async function createAndSeedPropertyPermissions(_0x50bcf5,_0x29178b){const _0x177292=await getResourceMeta(resourceNames['resourceProperty']),_0xb6e1aa=convertCrudActionToPermissionAction(getAllPermissionActions());for(const _0xd44c2d of await getAllResourceIds()){const _0x2fffe6=new ODataBuilder();_0x2fffe6['filter'](resourcePropertyProps['resourceId'],_0xd44c2d,oDataOperators['equal'],{'comparisonType':oDataComparisonTypes['equal']});const _0x3da2a4=await getOData(_0x177292['resourceName'],_0x177292['orderBys'],_0x2fffe6['build']()),_0x438eae=new Map();for(const _0x3f80d7 of _0x3da2a4){const _0x5540fd=new Map();for(const _0x24cce1 of _0xb6e1aa){const _0x552ca5={...createBaseResourceProperties(void(-0x86*-0x2f+-0x1449+-0xdd*0x5),_0x29178b),'type':permissionTypes['property'],'resourcePropertyId':_0x3f80d7['id'],'action':_0x24cce1};await createPermissionDirectlyInDb(_0x552ca5),_0x5540fd['set'](_0x24cce1['toString'](),_0x552ca5);}_0x438eae['set'](_0x3f80d7['name'],_0x5540fd);}_0x50bcf5['set'](_0xd44c2d,_0x438eae);}}__name(createAndSeedPropertyPermissions,'createAndSeedPropertyPermissions');async function seedSuperAdminPermissionPackageToPermission(_0x27966d){const _0x1cc73a=[],_0x5cca59=[resourceIds['paymentPlan']];addPermissionIds(_0x5cca59,getAllPermissionActions(),_0x1cc73a,_0x27966d),await seedPermissionPackageToPermissionIfNotExist(permissionPackages['superAdminId'],_0x1cc73a);}__name(seedSuperAdminPermissionPackageToPermission,'seedSuperAdminPermissionPackageToPermission');async function createAndSeedRoleToPackageIfNotExist(){const _0x202a57=new Map([[roleIds['adminRoleId'],[permissionPackages['generalId'],permissionPackages['adminId']]],[roleIds['userRoleId'],[permissionPackages['generalId']]],[roleIds['traineeRoleId'],[permissionPackages['generalId']]],[roleIds['superAdminRoleId'],[permissionPackages['superAdminId'],permissionPackages['adminId']]],[roleIds['workspaceAdminRoleId'],[permissionPackages['workspaceAdminId']]]]),_0x30e1ef=await getRoleToPermissionPackages();for(const [_0x41e373,_0x4b2bd3]of _0x202a57){for(const _0x43819a of _0x4b2bd3){const _0x3ad436=_0x30e1ef['some'](_0xf5faf3=>_0xf5faf3['roleId']===_0x41e373&&_0xf5faf3['permissionPackageId']===_0x43819a);!_0x3ad436&&await createRoleToPermissionPackageDirectlyInDb({'id':v4(),'roleId':_0x41e373,'permissionPackageId':_0x43819a,'createdByUserId':toolboxUserConstants['workerBotId'],'modifiedByUserId':toolboxUserConstants['workerBotId'],'createdDate':getNow(),'modifiedDate':getNow()});}}}__name(createAndSeedRoleToPackageIfNotExist,'createAndSeedRoleToPackageIfNotExist');async function seedCommonSubscriptionPermissions(_0x14ecae,_0x576cde,_0x45b97c,_0x3db960,_0x16fd79){const _0x446979=_0x16fd79??[];addCommonPermissions(_0x446979,_0x576cde),addSettingsPermissions(_0x446979,_0x576cde),_0x45b97c?.['length']&&addPermissionIds(_0x45b97c,permissionActions['view'],_0x446979,_0x576cde),_0x3db960?.['length']&&addPermissionIds(_0x3db960,getAllPermissionActions(),_0x446979,_0x576cde),await seedPermissionPackageToPermissionIfNotExist(_0x14ecae,_0x446979);}__name(seedCommonSubscriptionPermissions,'seedCommonSubscriptionPermissions');function addSettingsPermissions(_0x23a6e7,_0x54ae28){addPermissionIds([resourceIds['userSetting']],permissionActions['view'],_0x23a6e7,_0x54ae28),addPermissionIds([resourceIds['workspaceSetting']],permissionActions['view']|permissionActions['update'],_0x23a6e7,_0x54ae28);}__name(addSettingsPermissions,'addSettingsPermissions');

export { createAndSeedPermissions, createAndSeedPropertyPermissions, createAndSeedRoleToPermission, seedAdminPermissionPackageToPermission, seedCustomerPermissionPackageToPermission, seedGeneralPermissionPackageToPermission, seedPermissionPackages, seedPermissionsDirectlyInDb, seedResourcePermissions, seedSuperAdminPermissionPackageToPermission, seedWorkspaceAdminPermissionPackageToPermission };
