import Permission from "../../../Types/Permission";
import Text from "../../../Types/Text";
import { combineWithPrivacyClause } from "../PrivacyFilterUtil";
import { Raw } from "typeorm";
export function shouldBypassAlertEpisodePrivacy(props) {
    var _a;
    if (props.isRoot || props.isMasterAdmin) {
        return true;
    }
    if (props.tenantId && props.userTenantAccessPermission) {
        const tenantPerm = props.userTenantAccessPermission[props.tenantId.toString()];
        if ((_a = tenantPerm === null || tenantPerm === void 0 ? void 0 : tenantPerm.permissions) === null || _a === void 0 ? void 0 : _a.some((p) => {
            return (p.permission === Permission.ProjectOwner ||
                p.permission === Permission.ProjectAdmin);
        })) {
            return true;
        }
    }
    return false;
}
export function getAlertEpisodeSelfPrivacyRaw(props) {
    if (shouldBypassAlertEpisodePrivacy(props)) {
        return undefined;
    }
    if (!props.userId) {
        return Raw((alias) => {
            return `(${alias} IS NULL OR ${alias} = FALSE)`;
        });
    }
    const uidRid = "uidAEpiPriv_" + Text.generateRandomText(10);
    return Raw((alias) => {
        return (`(${alias} IS NULL OR ${alias} = FALSE OR ` +
            `"AlertEpisode"."_id" IN (SELECT aeou."alertEpisodeId" FROM "AlertEpisodeOwnerUser" aeou WHERE aeou."userId" = :${uidRid} AND aeou."deletedAt" IS NULL) OR ` +
            `"AlertEpisode"."_id" IN (SELECT aeot."alertEpisodeId" FROM "AlertEpisodeOwnerTeam" aeot INNER JOIN "TeamMember" tm ON tm."teamId" = aeot."teamId" WHERE tm."userId" = :${uidRid} AND tm."deletedAt" IS NULL AND aeot."deletedAt" IS NULL))`);
    }, {
        [uidRid]: props.userId.toString(),
    });
}
export function getAlertEpisodeRelatedRecordPrivacyRaw(props) {
    if (shouldBypassAlertEpisodePrivacy(props)) {
        return undefined;
    }
    if (!props.userId) {
        return Raw((alias) => {
            return `(${alias} IN (SELECT ae."_id" FROM "AlertEpisode" ae WHERE ae."deletedAt" IS NULL AND (ae."isPrivate" IS NULL OR ae."isPrivate" = FALSE)))`;
        });
    }
    const uidRid = "uidAEpiRel_" + Text.generateRandomText(10);
    return Raw((alias) => {
        return (`(${alias} IN (SELECT ae."_id" FROM "AlertEpisode" ae WHERE ae."deletedAt" IS NULL AND (` +
            `ae."isPrivate" IS NULL OR ae."isPrivate" = FALSE OR ` +
            `ae."_id" IN (SELECT aeou."alertEpisodeId" FROM "AlertEpisodeOwnerUser" aeou WHERE aeou."userId" = :${uidRid} AND aeou."deletedAt" IS NULL) OR ` +
            `ae."_id" IN (SELECT aeot."alertEpisodeId" FROM "AlertEpisodeOwnerTeam" aeot INNER JOIN "TeamMember" tm ON tm."teamId" = aeot."teamId" WHERE tm."userId" = :${uidRid} AND tm."deletedAt" IS NULL AND aeot."deletedAt" IS NULL))))`);
    }, {
        [uidRid]: props.userId.toString(),
    });
}
export function applyAlertEpisodeSelfPrivacyFilter(query, props) {
    const rawClause = getAlertEpisodeSelfPrivacyRaw(props);
    if (!rawClause) {
        return query;
    }
    if (!query) {
        return { isPrivate: rawClause };
    }
    query.isPrivate = combineWithPrivacyClause(query.isPrivate, rawClause);
    return query;
}
export function applyAlertEpisodeRelatedRecordPrivacyFilter(query, props) {
    const rawClause = getAlertEpisodeRelatedRecordPrivacyRaw(props);
    if (!rawClause) {
        return query;
    }
    if (!query) {
        return { alertEpisodeId: rawClause };
    }
    query.alertEpisodeId = combineWithPrivacyClause(query.alertEpisodeId, rawClause);
    return query;
}
//# sourceMappingURL=AlertEpisodePrivacyFilter.js.map