import Permission from "../../../Types/Permission";
import Text from "../../../Types/Text";
import { combineWithPrivacyClause } from "../PrivacyFilterUtil";
import { Raw } from "typeorm";
/*
 * Project owners and admins, plus root/master-admin contexts, see all
 * private incident episodes. Other users only see private episodes they
 * own (directly via IncidentEpisodeOwnerUser, or transitively through
 * IncidentEpisodeOwnerTeam + TeamMember).
 */
export function shouldBypassIncidentEpisodePrivacy(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 getIncidentEpisodeSelfPrivacyRaw(props) {
    if (shouldBypassIncidentEpisodePrivacy(props)) {
        return undefined;
    }
    if (!props.userId) {
        return Raw((alias) => {
            return `(${alias} IS NULL OR ${alias} = FALSE)`;
        });
    }
    const uidRid = "uidEpiPriv_" + Text.generateRandomText(10);
    return Raw((alias) => {
        return (`(${alias} IS NULL OR ${alias} = FALSE OR ` +
            `"IncidentEpisode"."_id" IN (SELECT ieou."incidentEpisodeId" FROM "IncidentEpisodeOwnerUser" ieou WHERE ieou."userId" = :${uidRid} AND ieou."deletedAt" IS NULL) OR ` +
            `"IncidentEpisode"."_id" IN (SELECT ieot."incidentEpisodeId" FROM "IncidentEpisodeOwnerTeam" ieot INNER JOIN "TeamMember" tm ON tm."teamId" = ieot."teamId" WHERE tm."userId" = :${uidRid} AND tm."deletedAt" IS NULL AND ieot."deletedAt" IS NULL))`);
    }, {
        [uidRid]: props.userId.toString(),
    });
}
export function getIncidentEpisodeRelatedRecordPrivacyRaw(props) {
    if (shouldBypassIncidentEpisodePrivacy(props)) {
        return undefined;
    }
    if (!props.userId) {
        return Raw((alias) => {
            return `(${alias} IN (SELECT ie."_id" FROM "IncidentEpisode" ie WHERE ie."deletedAt" IS NULL AND (ie."isPrivate" IS NULL OR ie."isPrivate" = FALSE)))`;
        });
    }
    const uidRid = "uidEpiRel_" + Text.generateRandomText(10);
    return Raw((alias) => {
        return (`(${alias} IN (SELECT ie."_id" FROM "IncidentEpisode" ie WHERE ie."deletedAt" IS NULL AND (` +
            `ie."isPrivate" IS NULL OR ie."isPrivate" = FALSE OR ` +
            `ie."_id" IN (SELECT ieou."incidentEpisodeId" FROM "IncidentEpisodeOwnerUser" ieou WHERE ieou."userId" = :${uidRid} AND ieou."deletedAt" IS NULL) OR ` +
            `ie."_id" IN (SELECT ieot."incidentEpisodeId" FROM "IncidentEpisodeOwnerTeam" ieot INNER JOIN "TeamMember" tm ON tm."teamId" = ieot."teamId" WHERE tm."userId" = :${uidRid} AND tm."deletedAt" IS NULL AND ieot."deletedAt" IS NULL))))`);
    }, {
        [uidRid]: props.userId.toString(),
    });
}
export function applyIncidentEpisodeSelfPrivacyFilter(query, props) {
    const rawClause = getIncidentEpisodeSelfPrivacyRaw(props);
    if (!rawClause) {
        return query;
    }
    if (!query) {
        return { isPrivate: rawClause };
    }
    query.isPrivate = combineWithPrivacyClause(query.isPrivate, rawClause);
    return query;
}
export function applyIncidentEpisodeRelatedRecordPrivacyFilter(query, props) {
    const rawClause = getIncidentEpisodeRelatedRecordPrivacyRaw(props);
    if (!rawClause) {
        return query;
    }
    if (!query) {
        return { incidentEpisodeId: rawClause };
    }
    query.incidentEpisodeId = combineWithPrivacyClause(query.incidentEpisodeId, rawClause);
    return query;
}
//# sourceMappingURL=IncidentEpisodePrivacyFilter.js.map