"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.canPostToChannel = canPostToChannel;
const sharingAccess_1 = require("../../enums/sharingAccess");
const portal_privilege_1 = require("../portal-privilege");
const constants_1 = require("../../../constants");
const ALLOWED_GROUP_ROLES = Object.freeze(["owner", "admin", "member"]);
// NO V2 EQUIVALENT. Use canCreatePostV2 or canCreateReplyV2
/**
 * Utility to determine if User has privileges to create a post in a channel
 * @deprecated use `canCreatePostV2` or 'canCreateReplyV2` instead
 * @export
 * @param channel
 * @param user
 * @returns {boolean}
 */
function canPostToChannel(channel, user = {}) {
    const { access, groups, orgs, allowAnonymous } = channel;
    if ((0, portal_privilege_1.hasOrgAdminUpdateRights)(user, channel.orgId)) {
        return true;
    }
    return isAuthorizedToPostByLegacyPermissions(user, {
        access,
        groups,
        orgs,
        allowAnonymous,
    });
}
function isAuthorizedToPostByLegacyPermissions(user, channelParams) {
    const { username, groups: userGroups, orgId: userOrgId } = user;
    const { allowAnonymous, access, groups, orgs } = channelParams;
    // order is important here
    if (allowAnonymous === true) {
        return true;
    }
    if (!username) {
        return false;
    }
    if (access === sharingAccess_1.SharingAccess.PUBLIC) {
        return true;
    }
    if (access === sharingAccess_1.SharingAccess.PRIVATE) {
        return isAuthorizedToPostByLegacyGroup(groups, userGroups);
    }
    if (access === sharingAccess_1.SharingAccess.ORG) {
        return orgs.includes(userOrgId);
    }
    return false;
}
function isAuthorizedToPostByLegacyGroup(channelGroups = [], userGroups = []) {
    return channelGroups.some((channelGroupId) => {
        return userGroups.some((group) => {
            const { id: userGroupId, userMembership: { memberType: userMemberType }, typeKeywords, } = group;
            return (channelGroupId === userGroupId &&
                ALLOWED_GROUP_ROLES.includes(userMemberType) &&
                !typeKeywords.includes(constants_1.CANNOT_DISCUSS));
        });
    });
}
//# sourceMappingURL=can-post-to-channel.js.map