"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.canReadChannel = canReadChannel;
exports.canReadFromChannel = canReadFromChannel;
const platform_1 = require("../platform");
const portal_privilege_1 = require("../portal-privilege");
/**
 * Utility to determine if User can view channel posts and channel attributes
 *
 * @deprecated replace with canReadChannelV2 for v2 discussions
 * @export
 * @param {IChannel} channel
 * @param {IUser} user
 * @return {*}  {boolean}
 */
function canReadChannel(channel, user = {}) {
    if ((0, portal_privilege_1.hasOrgAdminViewRights)(user, channel.orgId)) {
        return true;
    }
    if (channel.access === "private") {
        // ensure user is member of at least one group
        return intersectGroups(["member", "owner", "admin"])(user, channel);
    }
    if (channel.access === "org") {
        return (intersectGroups(["member", "owner", "admin"])(user, channel) ||
            isLegacyChannelOrgMember(channel, user));
    }
    // public channel
    return true;
}
/**
 * Utility (deprecated) to determine whether User can view posts belonging to Channel
 *
 * @export
 * @deprecated replace with canReadChannelV2 for v2 discussions
 * @param {IChannel} channel
 * @param {IUser} user
 * @return {*}  {boolean}
 */
function canReadFromChannel(channel, user = {}) {
    return canReadChannel(channel, user);
}
function intersectGroups(membershipTypes) {
    return (user, channel) => {
        const { groups: sharedGroups = [] } = channel;
        const { groups: userGroups = [] } = user;
        const eligibleUserGroups = userGroups.reduce((0, platform_1.reduceByGroupMembership)(membershipTypes), []);
        const method = "some";
        return sharedGroups[method]((group) => eligibleUserGroups.indexOf(group) > -1);
    };
}
function isLegacyChannelOrgMember(channel, user) {
    // orgs.length = 1 until collaboration/discussion between many orgs is ideated
    return channel.orgs.length === 1 && channel.orgs.indexOf(user.orgId) > -1;
}
//# sourceMappingURL=can-read-channel.js.map