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 DatabaseRequestType from "../../BaseDatabase/DatabaseRequestType";
import QueryHelper from "../QueryHelper";
import SelectUtil from "../SelectUtil";
import BasePermission from "./BasePermission";
import TablePermission from "./TablePermission";
import DatabaseCommonInteractionPropsUtil, { PermissionType, } from "../../../../Types/BaseDatabase/DatabaseCommonInteractionPropsUtil";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
export default class ReadPermission {
    static async checkReadPermission(modelType, query, select, props) {
        // check block permission first.
        await this.checkReadBlockPermission(modelType, query, props);
        const baseFunctionReturn = await BasePermission.checkPermissions(modelType, query, select, props, DatabaseRequestType.Read);
        // upate query
        query = baseFunctionReturn.query;
        let relationSelect = {};
        if (select) {
            const result = SelectUtil.sanitizeSelect(modelType, select);
            select = result.select;
            relationSelect = result.relationSelect;
        }
        return { query, select, relationSelect };
    }
    static async checkReadBlockPermission(modelType, query, props) {
        // If system is making this query then let the query run!
        if (props.isRoot || props.isMasterAdmin) {
            return { query };
        }
        TablePermission.checkTableLevelBlockPermissions(modelType, props, DatabaseRequestType.Read);
        const blockPermissionWithLabels = DatabaseCommonInteractionPropsUtil.getUserPermissions(props, PermissionType.Block).filter((permission) => {
            return permission.labelIds && permission.labelIds.length > 0;
        });
        if (blockPermissionWithLabels.length === 0) {
            return { query };
        }
        const modelPermissions = TablePermission.getTablePermission(modelType, DatabaseRequestType.Read);
        const blockPermissionsBelongToThisModel = blockPermissionWithLabels.filter((blockPermission) => {
            let isModelPermission = false;
            for (const permission of modelPermissions) {
                if (permission.toString() === blockPermission.permission.toString()) {
                    isModelPermission = true;
                    break;
                }
            }
            return isModelPermission;
        });
        if (blockPermissionsBelongToThisModel.length === 0) {
            return { query };
        }
        let labelIds = [];
        for (const blockPermissionBelongToThisModel of blockPermissionsBelongToThisModel) {
            if (blockPermissionBelongToThisModel.labelIds) {
                labelIds = [...labelIds, ...blockPermissionBelongToThisModel.labelIds];
            }
        }
        // now add these to query
        const model = new modelType();
        query[model.getAccessControlColumn()] = {
            _id: QueryHelper.notInOrNull(labelIds),
        };
        return { query };
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object, Object]),
    __metadata("design:returntype", Promise)
], ReadPermission, "checkReadPermission", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object]),
    __metadata("design:returntype", Promise)
], ReadPermission, "checkReadBlockPermission", null);
//# sourceMappingURL=ReadPermission.js.map