import { SharingAccess } from "../../enums/sharingAccess";
import { CANNOT_DISCUSS } from "../../../constants";
/**
 * Utility to determine if User has privileges to modify a post
 * @export
 * @deprecated replace with canEditPostV2 for v2 discussions
 * @param post
 * @param user
 * @param channel
 * @returns {boolean}
 */
export function canModifyPost(post, user = {}, channel) {
    return canEditPost(post, user, channel);
}
/**
 * Utility to determine if User has privileges to modify a post
 * @export
 * @deprecated replace with canEditPostV2 for v2 discussions
 * @param post
 * @param user
 * @param channel
 * @returns {boolean}
 */
export function canEditPost(post, user = {}, channel) {
    const { access, groups, orgs, allowAnonymous } = channel;
    return (isPostCreator(post, user) &&
        isAuthorizedToModifyByLegacyPermissions(user, {
            access,
            groups,
            orgs,
            allowAnonymous,
        }));
}
function isPostCreator(post, user) {
    return !!user.username && post.creator === user.username;
}
function isAuthorizedToModifyByLegacyPermissions(user, channelParams) {
    const { groups: userGroups = [], orgId: userOrgId } = user;
    const { access, groups: channelGroups = [], orgs = [] } = channelParams;
    if (access === SharingAccess.PUBLIC) {
        return true;
    }
    if (access === SharingAccess.ORG) {
        return (isAuthorizedToModifyPostByLegacyGroup(channelGroups, userGroups) ||
            orgs.includes(userOrgId));
    }
    // private
    return isAuthorizedToModifyPostByLegacyGroup(channelGroups, userGroups);
}
/**
 * Ensure the user is a member of one of the channel groups
 * and the group is not marked as non-discussable
 */
function isAuthorizedToModifyPostByLegacyGroup(channelGroups, userGroups) {
    return channelGroups.some((channelGroupId) => {
        return userGroups.some((group) => {
            const { id: userGroupId, typeKeywords = [] } = group;
            return (channelGroupId === userGroupId && !typeKeywords.includes(CANNOT_DISCUSS));
        });
    });
}
//# sourceMappingURL=can-edit-post.js.map