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 LIMIT_MAX from "../../../Types/Database/LimitMax";
import ObjectID from "../../../Types/ObjectID";
import Permission from "../../../Types/Permission";
import ApiKeyPermissionService from "../../Services/ApiKeyPermissionService";
import CaptureSpan from "../Telemetry/CaptureSpan";
import UserPermissionUtil from "../UserPermission/UserPermission";
export default class APIKeyAccessPermission {
    static async getDefaultApiGlobalPermission(projectId) {
        return {
            projectIds: [projectId],
            globalPermissions: [
                Permission.Public,
                Permission.User,
                Permission.CurrentUser,
            ],
            _type: "UserGlobalAccessPermission",
        };
    }
    static async getMasterKeyApiGlobalPermission(projectId) {
        return {
            projectIds: [projectId],
            globalPermissions: [
                Permission.Public,
                Permission.User,
                Permission.CurrentUser,
                Permission.ProjectOwner,
            ],
            _type: "UserGlobalAccessPermission",
        };
    }
    static async getMasterApiTenantAccessPermission(projectId) {
        const userPermissions = [];
        userPermissions.push({
            permission: Permission.ProjectOwner,
            labelIds: [],
            _type: "UserPermission",
        });
        const permission = UserPermissionUtil.getDefaultUserTenantAccessPermission(projectId);
        permission.permissions = permission.permissions.concat(userPermissions);
        return permission;
    }
    static async getApiTenantAccessPermission(projectId, apiKeyId) {
        // get team permissions.
        const apiKeyPermission = await ApiKeyPermissionService.findBy({
            query: {
                apiKeyId: apiKeyId,
            },
            select: {
                permission: true,
                labels: {
                    _id: true,
                },
                isBlockPermission: true,
            },
            limit: LIMIT_MAX,
            skip: 0,
            props: {
                isRoot: true,
            },
        });
        const userPermissions = [];
        for (const apiPermission of apiKeyPermission) {
            if (!apiPermission.labels) {
                apiPermission.labels = [];
            }
            userPermissions.push({
                permission: apiPermission.permission,
                labelIds: apiPermission.labels.map((label) => {
                    return label.id;
                }),
                isBlockPermission: apiPermission.isBlockPermission,
                _type: "UserPermission",
            });
        }
        const permission = UserPermissionUtil.getDefaultUserTenantAccessPermission(projectId);
        permission.permissions = permission.permissions.concat(userPermissions);
        return permission;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID]),
    __metadata("design:returntype", Promise)
], APIKeyAccessPermission, "getDefaultApiGlobalPermission", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID]),
    __metadata("design:returntype", Promise)
], APIKeyAccessPermission, "getMasterKeyApiGlobalPermission", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID]),
    __metadata("design:returntype", Promise)
], APIKeyAccessPermission, "getMasterApiTenantAccessPermission", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [ObjectID,
        ObjectID]),
    __metadata("design:returntype", Promise)
], APIKeyAccessPermission, "getApiTenantAccessPermission", null);
//# sourceMappingURL=AccessPermission.js.map