"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformEntityToChannelData = transformEntityToChannelData;
const aclCategory_1 = require("../../discussions/api/enums/aclCategory");
const aclSubCategory_1 = require("../../discussions/api/enums/aclSubCategory");
const role_1 = require("../../discussions/api/enums/role");
const IEntityPermissionPolicy_1 = require("../../permissions/types/IEntityPermissionPolicy");
const ChannelBusinessRules_1 = require("./ChannelBusinessRules");
const PERMISSION_TO_ROLE_MAP = {
    [ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelRead]: role_1.Role.READ,
    [ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelWrite]: role_1.Role.WRITE,
    [ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelReadWrite]: role_1.Role.READWRITE,
    [ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelManage]: role_1.Role.MANAGE,
    [ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelModerate]: role_1.Role.MODERATE,
    [ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelOwner]: role_1.Role.OWNER,
};
const COLLABORATION_TYPE_TO_PERMISSION_DEFINTION_MEMBERS_MAP = {
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.anonymous]: {
        category: aclCategory_1.AclCategory.ANONYMOUS_USER,
        subCategory: null,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.authenticated]: {
        category: aclCategory_1.AclCategory.AUTHENTICATED_USER,
        subCategory: null,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.group]: {
        category: aclCategory_1.AclCategory.GROUP,
        subCategory: aclSubCategory_1.AclSubCategory.MEMBER,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.groupAdmin]: {
        category: aclCategory_1.AclCategory.GROUP,
        subCategory: aclSubCategory_1.AclSubCategory.ADMIN,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.org]: {
        category: aclCategory_1.AclCategory.ORG,
        subCategory: aclSubCategory_1.AclSubCategory.MEMBER,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.orgAdmin]: {
        category: aclCategory_1.AclCategory.ORG,
        subCategory: aclSubCategory_1.AclSubCategory.ADMIN,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.owner]: {
        category: aclCategory_1.AclCategory.USER,
        subCategory: null,
    },
    [IEntityPermissionPolicy_1.COLLABORATION_TYPES.user]: {
        category: aclCategory_1.AclCategory.USER,
        subCategory: null,
    },
};
/**
 * @private
 * Transforms an IHubChannel entity pojo object to an object that can be passed V2 channel create and update methods
 * @param entity an IHubChannel object
 * @returns an object that can be passed to V2 channel create and update methods
 */
function transformEntityToChannelData(entity) {
    function toChannelAclDefinition(acc, permissionPolicy) {
        const permissionDefinition = COLLABORATION_TYPE_TO_PERMISSION_DEFINTION_MEMBERS_MAP[permissionPolicy.collaborationType];
        return permissionDefinition
            ? [
                ...acc,
                Object.assign(Object.assign({}, permissionDefinition), { key: permissionPolicy.collaborationId || null, role: PERMISSION_TO_ROLE_MAP[permissionPolicy.permission] }),
            ]
            : acc;
    }
    return {
        name: entity.name,
        blockWords: entity.blockWords,
        allowAsAnonymous: entity.allowAsAnonymous,
        channelAclDefinition: entity.permissions.reduce(toChannelAclDefinition, []),
    };
}
//# sourceMappingURL=transformEntityToChannelData.js.map