import { SharingAccess } from "../../enums/sharingAccess";
import { isOrgAdmin } from "../platform";
import { hasOrgAdminUpdateRights } from "../portal-privilege";
const ADMIN_GROUP_ROLES = Object.freeze(["owner", "admin"]);
/**
 * Utility to determine if User has privileges to modify the status of a post
 * @export
 * @deprecated replace with canEditPostStatusV2 for v2 discussions
 * @param channel
 * @param user
 * @returns {boolean}
 */
export function canModifyPostStatus(channel, user = {}) {
    return canEditPostStatus(channel, user);
}
/**
 * Utility to determine if User has privileges to modify the status of a post
 * @export
 * @deprecated replace with canEditPostStatusV2 for v2 discussions
 * @param channel
 * @param user
 * @returns {boolean}
 */
export function canEditPostStatus(channel, user = {}) {
    if (hasOrgAdminUpdateRights(user, channel.orgId)) {
        return true;
    }
    return isAuthorizedToModifyStatusByLegacyPermissions(user, channel);
}
function isAuthorizedToModifyStatusByLegacyPermissions(user, channel) {
    const { username, groups: userGroups = [] } = user;
    const { access, groups: channelGroups = [], orgs: channelOrgs = [], creator: channelCreator, } = channel;
    if (!username) {
        return false;
    }
    if (channelCreator === username) {
        return true;
    }
    if (access === SharingAccess.PRIVATE) {
        return isAuthorizedToModifyStatusByLegacyGroup(channelGroups, userGroups);
    }
    // public or org access
    return (isAuthorizedToModifyStatusByLegacyGroup(channelGroups, userGroups) ||
        isLegacyChannelOrgAdmin(channelOrgs, user));
}
/**
 * Ensure the user is an owner/admin of one of the channel groups
 */
function isAuthorizedToModifyStatusByLegacyGroup(channelGroups, userGroups) {
    return channelGroups.some((channelGroupId) => {
        return userGroups.some((group) => {
            const { id: userGroupId, userMembership: { memberType: userMemberType }, } = group;
            return (channelGroupId === userGroupId &&
                ADMIN_GROUP_ROLES.includes(userMemberType));
        });
    });
}
function isLegacyChannelOrgAdmin(channelOrgs, user) {
    return isOrgAdmin(user) && channelOrgs.includes(user.orgId);
}
//# sourceMappingURL=can-edit-post-status.js.map