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 ColumnPermissions from "./ColumnPermission";
import DatabaseCommonInteractionPropsUtil, { PermissionType, } from "../../../../Types/BaseDatabase/DatabaseCommonInteractionPropsUtil";
import BadDataException from "../../../../Types/Exception/BadDataException";
import NotAuthorizedException from "../../../../Types/Exception/NotAuthorizedException";
import { PermissionHelper } from "../../../../Types/Permission";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
export default class SelectPermission {
    static checkSelectPermission(modelType, select, props) {
        const model = new modelType();
        const userPermissions = DatabaseCommonInteractionPropsUtil.getUserPermissions(props, PermissionType.Allow);
        const canReadOnTheseColumns = ColumnPermissions.getModelColumnsByPermissions(modelType, userPermissions || [], DatabaseRequestType.Read);
        const tableColumns = model.getTableColumns().columns;
        const excludedColumnNames = ColumnPermissions.getExcludedColumnNames();
        for (const key in select) {
            if (excludedColumnNames.includes(key)) {
                continue;
            }
            if (!canReadOnTheseColumns.columns.includes(key)) {
                if (!tableColumns.includes(key)) {
                    throw new BadDataException(`Invalid select clause. Cannot select on "${key}". This column does not exist on ${model.singularName}. Here are the columns you can select on instead: ${tableColumns.join(", ")}`);
                }
                throw new NotAuthorizedException(`You do not have permissions to select on - ${key}.
                    You need any one of these permissions: ${PermissionHelper.getPermissionTitles(model.getColumnAccessControlFor(key)
                    ? model.getColumnAccessControlFor(key).read
                    : []).join(", ")}`);
            }
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object]),
    __metadata("design:returntype", void 0)
], SelectPermission, "checkSelectPermission", null);
//# sourceMappingURL=SelectPermission.js.map