var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import ObjectID from "../../../Types/ObjectID";
import Permission from "../../../Types/Permission";
import GlobalCache from "../../Infrastructure/GlobalCache";
import PermissionNamespace from "../../Types/Permission/PermissionNamespace";
import CaptureSpan from "../Telemetry/CaptureSpan";
export default class UserPermissionUtil {
    /*
     * Build the cache key for a (user, project) tenant-permission entry.
     * The previous shape was `userId.toString() + projectId.toString()`
     * with no separator — two distinct ObjectID pairs could in principle
     * collide because plain concatenation has no boundary marker. Use a
     * delimiter so the namespace is unambiguous, and route both the GET
     * (here) and the SET (in `AccessTokenService.refreshUserTenant
     * AccessPermission`) through this helper so they can't drift.
     */
    static buildTenantPermissionCacheKey(userId, projectId) {
        return `${userId.toString()}:${projectId.toString()}`;
    }
    static async getUserTenantAccessPermissionFromCache(userId, projectId) {
        const json = (await GlobalCache.getJSONObject(PermissionNamespace.ProjectPermission, this.buildTenantPermissionCacheKey(userId, projectId)));
        if (json) {
            json._type = "UserTenantAccessPermission";
        }
        if (!json) {
            return null;
        }
        return json;
    }
    static async getUserGlobalAccessPermissionFromCache(userId) {
        const json = await GlobalCache.getJSONObject("user", userId.toString());
        if (!json) {
            return null;
        }
        const accessPermission = json;
        accessPermission._type = "UserGlobalAccessPermission";
        return accessPermission;
    }
    static getDefaultUserTenantAccessPermission(projectId) {
        const userPermissions = [];
        userPermissions.push({
            permission: Permission.CurrentUser,
            labelIds: [],
            isBlockPermission: false,
            _type: "UserPermission",
        });
        userPermissions.push({
            permission: Permission.UnAuthorizedSsoUser,
            labelIds: [],
            isBlockPermission: false,
            _type: "UserPermission",
        });
        const permission = {
            projectId,
            permissions: userPermissions,
            isBlockPermission: false,
            _type: "UserTenantAccessPermission",
        };
        return permission;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID,
        ObjectID]),
    __metadata("design:returntype", Promise)
], UserPermissionUtil, "getUserTenantAccessPermissionFromCache", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID]),
    __metadata("design:returntype", Promise)
], UserPermissionUtil, "getUserGlobalAccessPermissionFromCache", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID]),
    __metadata("design:returntype", Object)
], UserPermissionUtil, "getDefaultUserTenantAccessPermission", null);
//# sourceMappingURL=UserPermission.js.map