"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformFormValuesToEntityPermissionPolicies = transformFormValuesToEntityPermissionPolicies;
const IEntityPermissionPolicy_1 = require("../../permissions/types/IEntityPermissionPolicy");
const ChannelBusinessRules_1 = require("./ChannelBusinessRules");
/**
 * @private
 * Transforms an array of IHubRoleConfigValue objects into an array of IEntityPermissionPolicy objects
 * @param roleConfigs an array of IHubRoleConfigValue objects
 * @returns an array of IEntityPermissionPolicy objects
 */
function transformFormValuesToEntityPermissionPolicies(roleConfigs) {
    return roleConfigs.reduce((acc, roleConfiguration) => {
        return Object.entries(roleConfiguration.roles).reduce((memo, [roleKey, role]) => {
            if (role.value === ChannelBusinessRules_1.ChannelNonePermission) {
                return memo;
            }
            let permissionPolicy;
            if (roleConfiguration.key === "public") {
                permissionPolicy = {
                    collaborationType: roleKey === "authenticated"
                        ? IEntityPermissionPolicy_1.COLLABORATION_TYPES.authenticated
                        : IEntityPermissionPolicy_1.COLLABORATION_TYPES.anonymous,
                    collaborationId: null,
                };
            }
            else if (roleConfiguration.entityType === "group") {
                permissionPolicy = {
                    collaborationType: roleKey === "admin"
                        ? IEntityPermissionPolicy_1.COLLABORATION_TYPES.groupAdmin
                        : IEntityPermissionPolicy_1.COLLABORATION_TYPES.group,
                    collaborationId: roleConfiguration.entityId,
                };
            }
            else if (roleConfiguration.entityType === "organization") {
                permissionPolicy = {
                    collaborationType: roleKey === "admin"
                        ? IEntityPermissionPolicy_1.COLLABORATION_TYPES.orgAdmin
                        : IEntityPermissionPolicy_1.COLLABORATION_TYPES.org,
                    collaborationId: roleConfiguration.entityId,
                };
            }
            else if (roleConfiguration.entityType === "user") {
                permissionPolicy = {
                    collaborationType: IEntityPermissionPolicy_1.COLLABORATION_TYPES.user,
                    collaborationId: roleConfiguration.entityId,
                };
            }
            return permissionPolicy
                ? [
                    ...memo,
                    Object.assign(Object.assign({}, permissionPolicy), { permission: role.value, id: role.id }),
                ]
                : memo;
        }, acc);
    }, []);
}
//# sourceMappingURL=transformFormValuesToEntityPermissionPolicies.js.map