import { COLLABORATION_TYPES, } from "../../permissions/types/IEntityPermissionPolicy";
import { ChannelNonePermission } from "./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
 */
export function transformFormValuesToEntityPermissionPolicies(roleConfigs) {
    return roleConfigs.reduce((acc, roleConfiguration) => {
        return Object.entries(roleConfiguration.roles).reduce((memo, [roleKey, role]) => {
            if (role.value === ChannelNonePermission) {
                return memo;
            }
            let permissionPolicy;
            if (roleConfiguration.key === "public") {
                permissionPolicy = {
                    collaborationType: roleKey === "authenticated"
                        ? COLLABORATION_TYPES.authenticated
                        : COLLABORATION_TYPES.anonymous,
                    collaborationId: null,
                };
            }
            else if (roleConfiguration.entityType === "group") {
                permissionPolicy = {
                    collaborationType: roleKey === "admin"
                        ? COLLABORATION_TYPES.groupAdmin
                        : COLLABORATION_TYPES.group,
                    collaborationId: roleConfiguration.entityId,
                };
            }
            else if (roleConfiguration.entityType === "organization") {
                permissionPolicy = {
                    collaborationType: roleKey === "admin"
                        ? COLLABORATION_TYPES.orgAdmin
                        : COLLABORATION_TYPES.org,
                    collaborationId: roleConfiguration.entityId,
                };
            }
            else if (roleConfiguration.entityType === "user") {
                permissionPolicy = {
                    collaborationType: 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