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 { IsBillingEnabled } from "../EnvironmentConfig";
import ProjectService from "../Services/ProjectService";
import UserType from "../../Types/UserType";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import SpanUtil from "../Utils/Telemetry/SpanUtil";
export default class CommonAPI {
    static async getDatabaseCommonInteractionProps(req) {
        var _a;
        const props = {
            tenantId: undefined,
            userGlobalAccessPermission: undefined,
            userTenantAccessPermission: undefined,
            userId: undefined,
            userType: req.userType,
            isMultiTenantRequest: undefined,
        };
        if (req.userAuthorization &&
            ((_a = req.userAuthorization) === null || _a === void 0 ? void 0 : _a.userId)) {
            props.userId = req.userAuthorization.userId;
        }
        if (req.userGlobalAccessPermission) {
            props.userGlobalAccessPermission = req.userGlobalAccessPermission;
        }
        if (req.userTenantAccessPermission) {
            props.userTenantAccessPermission = req.userTenantAccessPermission;
        }
        if (req.userTeamIds) {
            props.userTeamIds = req.userTeamIds;
        }
        if (req.tenantId) {
            props.tenantId = req.tenantId || undefined;
        }
        if (req.headers["is-multi-tenant-query"]) {
            props.isMultiTenantRequest = true;
        }
        if (IsBillingEnabled && props.tenantId) {
            const plan = await ProjectService.getCurrentPlan(props.tenantId);
            props.currentPlan = plan.plan || undefined;
            props.isSubscriptionUnpaid = plan.isSubscriptionUnpaid;
        }
        // check for root permissions.
        if (props.userType === UserType.MasterAdmin) {
            props.isMasterAdmin = true;
        }
        // Add context attributes to the current span for observability
        SpanUtil.addAttributesToCurrentSpan(Object.assign(Object.assign(Object.assign(Object.assign({}, (props.tenantId ? { projectId: props.tenantId.toString() } : {})), (props.userId ? { userId: props.userId.toString() } : {})), (props.userType ? { userType: props.userType } : {})), (req.requestId
            ? { requestId: req.requestId }
            : {})));
        return props;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], CommonAPI, "getDatabaseCommonInteractionProps", null);
//# sourceMappingURL=CommonAPI.js.map