import { ApiPermissions , hasPermission , TPermissionHierarchy } from "@shared/apiPermissions" ; export type TPermissionHierarchyState = { locked : boolean ; redundant : boolean ; } ; export type TApiPermissionWithState = [ ApiPermissions , TPermissionHierarchyState , TPermissionHierarchyWithState ? ] ; export type TPermissionHierarchyWithState = TApiPermissionWithState [ ] ; param tree param grantedPermissions param managerPermissions param entireTreeIsGranted export function applyStateToPermissionHierarchy ( tree , grantedPermissions , managerPermissions = new Set ( ) , entireTreeIsGranted = false ) { const result = [ ] ; for ( const item of tree ) { const [ perm , nested ] = Array . isArray ( item ) ? item : [ item ] ; const locked = ! hasPermission ( managerPermissions , perm ) ; const permissionWithState = [ perm , { locked , redundant : entireTreeIsGranted } ] ; if ( nested ) { const subTreeGranted = entireTreeIsGranted || grantedPermissions . has ( perm ) ; permissionWithState . push ( applyStateToPermissionHierarchy ( nested , grantedPermissions , managerPermissions , subTreeGranted ) ) ; } result . push ( permissionWithState ) ; } return result ; }