"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.canCreatePost = canCreatePost;
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"]);
/**
 * Utility to determine if User has privileges to create a post in a channel
 *
 * @deprecated replace with canCreatePostV2 for v2 discussions
 * @export
 * @param channel
 * @param user
 * @returns {boolean}
 */
function canCreatePost(channel, user = {}) {
    const { access, groups, orgs } = channel;
    if ((0, portal_privilege_1.hasOrgAdminUpdateRights)(user, channel.orgId)) {
        return true;
    }
    return isAuthorizedToPostByLegacyPermissions(user, {
        access,
        groups,
        orgs,
    });
}
function isAuthorizedToPostByLegacyPermissions(user, channelParams) {
    const { username, groups: userGroups, orgId: userOrgId } = user;
    const { access, groups, orgs } = channelParams;
    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-create-post.js.map