'use strict';
import { apiGet } from '../../../../fd-toolbox/api/api-client.js';
import { apiPathNames } from '../../../../fd-toolbox/api/api-path-names.js';
import { crudActions } from '../../../../fd-toolbox/enums/enums.js';
import { permissionLevels, permissionActions } from '../../core/resources/enums/permission-enums.js';
import { isNullOrUndefined, isUuidSet } from '../../../../fd-toolbox/functions/value-checking-functions.js';
import { getLoginState } from '../../../../fd-toolbox/auth/internal-login-states.js';
import { getFlagArrayForFlagNumberFomEnum } from '../../../../fd-toolbox/collections/internal-enums.js';
import { notifyPermissionError } from '../../../../fd-toolbox/auth/permissions/permission-notifiers.js';
import { emptyUuid } from '../../../../fd-toolbox/constants/constants.js';
import { crudActionToPermissionPrefix } from '../../../../fd-toolbox/permissions/constants/crud-action-to-permission-prefixes.js';

var __defProp=Object['defineProperty'],__name=(_0x4d17a9,_0x22b45d)=>__defProp(_0x4d17a9,'name',{'value':_0x22b45d,'configurable':!![]});const userResourcePermission=new Map(),userPropertyPermission=new Map(),userDefaultPermission=new Map();async function loadPermissions(_0x3ce78e){if(_0x3ce78e)setPermissions(_0x3ce78e);else {let _0xfbab1f=await apiGet(apiPathNames['userPermission']);if(_0xfbab1f['data'])setPermissions(_0xfbab1f['data']);}}__name(loadPermissions,'loadPermissions');function setPermissions(_0xbc44da){for(const _0x76bef0 of Object['keys'](_0xbc44da['resourcePermissions'])){userResourcePermission['set'](_0x76bef0,_0xbc44da['resourcePermissions'][_0x76bef0]);}for(const _0x3c51c8 of Object['keys'](_0xbc44da['propertyPermissions'])){userPropertyPermission['set'](_0x3c51c8,_0xbc44da['propertyPermissions'][_0x3c51c8]);}for(const _0x21d828 of Object['keys'](_0xbc44da['defaultPermissions'])){userDefaultPermission['set'](_0x21d828,_0xbc44da['defaultPermissions'][_0x21d828]);}}__name(setPermissions,'setPermissions');function removePermissions(){userResourcePermission['clear'](),userPropertyPermission['clear'](),userDefaultPermission['clear']();}__name(removePermissions,'removePermissions');function hasPermission(_0x14bb5e,_0x119a07=permissionLevels['three']){if(!isNullOrUndefined(userDefaultPermission)){let _0x69571f=userDefaultPermission['get'](_0x14bb5e);return _0x69571f?.['name']===_0x14bb5e&&_0x69571f?.['level']>=_0x119a07;}return ![];}__name(hasPermission,'hasPermission');function hasResourcePermission(_0x26ad90,_0xfe47f6,_0x579886=permissionLevels['three']){if(!isNullOrUndefined(userResourcePermission)){let _0x19c21c=userResourcePermission['get'](_0x26ad90);if(_0x19c21c?.['action'])return getFlagArrayForFlagNumberFomEnum(_0x19c21c['action'],permissionActions)['includes'](convertCrudActionToPermissionAction(_0xfe47f6))&&_0x19c21c['level']>=_0x579886;}return ![];}__name(hasResourcePermission,'hasResourcePermission');function hasPropertyPermission(_0x199d0c,_0x324e90,_0x2bdf75=permissionLevels['three'],_0xf16908,_0x378e38){let _0x1bd2bc=![];if(!isNullOrUndefined(userPropertyPermission)){let _0x49f8ad=userPropertyPermission['get'](_0x199d0c);if(isNullOrUndefined(_0x49f8ad))_0x1bd2bc=!![];else _0x49f8ad&&(getFlagArrayForFlagNumberFomEnum(_0x49f8ad['action'],permissionActions)['includes'](convertCrudActionToPermissionAction(_0x324e90))&&_0x49f8ad['level']>=_0x2bdf75&&(_0x1bd2bc=!![]));}return !_0x1bd2bc&&_0xf16908&&_0x378e38&&_0xf16908['auth']['accessByUserId']&&(_0x1bd2bc=isActionAllowedWithId(_0x324e90,_0xf16908,_0x378e38)),_0x1bd2bc;}__name(hasPropertyPermission,'hasPropertyPermission');function hasCrudPermission(_0x5e9f67,_0x12aa46,_0x279643){let _0x365805=_0x279643,_0xeeb939=![];if(isActionSupported(_0x5e9f67,_0x12aa46)){if(hasFullPermission(_0x12aa46['id'],_0x5e9f67))_0xeeb939=!![];else !_0x279643?_0xeeb939=isCreateActionAllowed(_0x5e9f67,_0x12aa46):_0xeeb939=isActionAllowedWithId(_0x5e9f67,_0x12aa46,_0x365805);}return _0xeeb939;}__name(hasCrudPermission,'hasCrudPermission');function isBelowLevelCouldBeAllowedByUserId(_0x192d38,_0xfcd8f8){if(!_0xfcd8f8['supportedCrudActions']['includes'](_0x192d38))return ![];return _0xfcd8f8['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x192d38);}__name(isBelowLevelCouldBeAllowedByUserId,'isBelowLevelCouldBeAllowedByUserId');function notifyCrudPermissionError(_0x1f95ae){notifyPermissionError('You\x20don\x27t\x20have\x20permission\x20to\x20'+crudActionToPermissionPrefix['get'](_0x1f95ae)['toLowerCase']()+'\x20this\x20resource');}__name(notifyCrudPermissionError,'notifyCrudPermissionError');function convertCrudActionToPermissionAction(_0x1c338){let _0x30ce69=permissionActions['none'];if(_0x1c338===crudActions['create'])_0x30ce69=permissionActions['create'];else {if(_0x1c338===crudActions['delete'])_0x30ce69=permissionActions['delete'];else {if(_0x1c338===crudActions['update'])_0x30ce69=permissionActions['update'];else _0x1c338===crudActions['view']&&(_0x30ce69=permissionActions['view']);}}return _0x30ce69;}__name(convertCrudActionToPermissionAction,'convertCrudActionToPermissionAction');function isActionSupported(_0x1d3818,_0x1e1974){return _0x1e1974['supportedCrudActions']['includes'](_0x1d3818);}__name(isActionSupported,'isActionSupported');function hasFullPermission(_0x160b10,_0xd2040b){return hasResourcePermission(_0x160b10,_0xd2040b,permissionLevels['three']);}__name(hasFullPermission,'hasFullPermission');function isCreateActionAllowed(_0x57fee7,_0x5c07e0){return _0x57fee7===crudActions['create']&&_0x5c07e0['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x57fee7);}__name(isCreateActionAllowed,'isCreateActionAllowed');function isActionAllowedWithId(_0x2295d2,_0x3b5c85,_0x13626c){if(_0x13626c&&_0x3b5c85['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x2295d2)&&isUuidSet(_0x13626c['userId']))return _0x13626c['userId']===getLoginState()['userId'];return !!(_0x13626c&&_0x13626c['id']===emptyUuid&&_0x3b5c85['auth']['belowLevelAllowedCrudByUserId']['includes'](_0x2295d2));}__name(isActionAllowedWithId,'isActionAllowedWithId');

export { hasCrudPermission, hasPermission, hasPropertyPermission, hasResourcePermission, isBelowLevelCouldBeAllowedByUserId, loadPermissions, notifyCrudPermissionError, removePermissions, setPermissions };
