"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformEntityPermissionPoliciesToUserFormValues = exports.transformEntityPermissionPoliciesToOrgFormValues = exports.transformEntityPermissionPoliciesToGroupFormValues = exports.transformEntityPermissionPoliciesToPublicFormValues = void 0;
const IEntityPermissionPolicy_1 = require("../../permissions/types/IEntityPermissionPolicy");
const ChannelBusinessRules_1 = require("./ChannelBusinessRules");
/**
 * @private
 * Transforms an array of IEntityPermissionPolicy (Hub entity) objects to an array of IHubRoleConfigValue (entity editor) objects
 * representing the channel's public (anonymous & authenticated) permissions
 * @param permissionPolicies An array of IEntityPermissionPolicy objects
 * @returns an array of IHubRoleConfigValue objects
 */
const transformEntityPermissionPoliciesToPublicFormValues = (permissionPolicies) => {
    const anonymousPermission = permissionPolicies.find(({ collaborationType }) => collaborationType === IEntityPermissionPolicy_1.COLLABORATION_TYPES.anonymous);
    const authenticatedPermission = permissionPolicies.find(({ collaborationType }) => collaborationType === IEntityPermissionPolicy_1.COLLABORATION_TYPES.authenticated);
    return [
        {
            key: "public",
            roles: {
                anonymous: {
                    value: (anonymousPermission === null || anonymousPermission === void 0 ? void 0 : anonymousPermission.permission) || ChannelBusinessRules_1.ChannelNonePermission,
                    id: anonymousPermission === null || anonymousPermission === void 0 ? void 0 : anonymousPermission.id,
                },
                authenticated: {
                    value: (authenticatedPermission === null || authenticatedPermission === void 0 ? void 0 : authenticatedPermission.permission) || ChannelBusinessRules_1.ChannelNonePermission,
                    id: authenticatedPermission === null || authenticatedPermission === void 0 ? void 0 : authenticatedPermission.id,
                },
            },
        },
    ];
};
exports.transformEntityPermissionPoliciesToPublicFormValues = transformEntityPermissionPoliciesToPublicFormValues;
/**
 * @private
 * Transforms an array of IEntityPermissionPolicy (Hub entity) objects to an array of IHubRoleConfigValue (entity editor) objects
 * representing the channel's group (member & admin) permissions
 * @param permissionPolicies An array of IEntityPermissionPolicy objects
 * @returns an array of IHubRoleConfigValue objects
 */
const transformEntityPermissionPoliciesToGroupFormValues = (permissionPolicies) => {
    const permissionsByGroup = permissionPolicies.reduce((acc, permissionPolicy) => {
        if ([IEntityPermissionPolicy_1.COLLABORATION_TYPES.group, IEntityPermissionPolicy_1.COLLABORATION_TYPES.groupAdmin].includes(permissionPolicy.collaborationType)) {
            return Object.assign(Object.assign({}, acc), { [permissionPolicy.collaborationId]: Object.assign(Object.assign({}, acc[permissionPolicy.collaborationId]), { [permissionPolicy.collaborationType]: permissionPolicy }) });
        }
        else {
            return acc;
        }
    }, {});
    return Object.entries(permissionsByGroup).reduce((acc, [groupId, { group, ["group-admin"]: groupAdmin }]) => {
        return [
            ...acc,
            {
                key: groupId,
                entityId: groupId,
                entityType: "group",
                roles: {
                    member: {
                        value: (group === null || group === void 0 ? void 0 : group.permission) || ChannelBusinessRules_1.ChannelNonePermission,
                        id: group === null || group === void 0 ? void 0 : group.id,
                    },
                    admin: {
                        value: (groupAdmin === null || groupAdmin === void 0 ? void 0 : groupAdmin.permission) || ChannelBusinessRules_1.ChannelNonePermission,
                        id: groupAdmin === null || groupAdmin === void 0 ? void 0 : groupAdmin.id,
                    },
                },
            },
        ];
    }, []);
};
exports.transformEntityPermissionPoliciesToGroupFormValues = transformEntityPermissionPoliciesToGroupFormValues;
/**
 * @private
 * Transforms an array of IEntityPermissionPolicy (Hub entity) objects to an array of IHubRoleConfigValue (entity editor) objects
 * representing the channel's org (member & admin) permissions
 * @param permissionPolicies An array of IEntityPermissionPolicy objects
 * @param defaultOrgId The current user's orgId
 * @returns an array of IHubRoleConfigValue objects
 */
const transformEntityPermissionPoliciesToOrgFormValues = (permissionPolicies, defaultOrgId) => {
    const permissionsByOrg = permissionPolicies.reduce((acc, permissionPolicy) => {
        if ([IEntityPermissionPolicy_1.COLLABORATION_TYPES.org, IEntityPermissionPolicy_1.COLLABORATION_TYPES.orgAdmin].includes(permissionPolicy.collaborationType)) {
            return Object.assign(Object.assign({}, acc), { [permissionPolicy.collaborationId]: Object.assign(Object.assign({}, acc[permissionPolicy.collaborationId]), { [permissionPolicy.collaborationType]: permissionPolicy }) });
        }
        else {
            return acc;
        }
    }, {});
    const orgConfigs = Object.entries(permissionsByOrg).reduce((acc, [orgId, { org, ["org-admin"]: orgAdmin }]) => {
        return [
            ...acc,
            {
                key: orgId,
                entityId: orgId,
                entityType: "organization",
                roles: {
                    member: {
                        value: (org === null || org === void 0 ? void 0 : org.permission) || ChannelBusinessRules_1.ChannelNonePermission,
                        id: org === null || org === void 0 ? void 0 : org.id,
                    },
                    admin: {
                        value: (orgAdmin === null || orgAdmin === void 0 ? void 0 : orgAdmin.permission) || ChannelBusinessRules_1.ChannelNonePermission,
                        id: orgAdmin === null || orgAdmin === void 0 ? void 0 : orgAdmin.id,
                    },
                },
            },
        ];
    }, []);
    if (!orgConfigs.length) {
        orgConfigs.push({
            key: defaultOrgId,
            entityId: defaultOrgId,
            entityType: "organization",
            roles: {
                member: {
                    value: ChannelBusinessRules_1.ChannelNonePermission,
                    id: undefined,
                },
                admin: {
                    value: ChannelBusinessRules_1.CHANNEL_PERMISSIONS.channelOwner,
                    id: undefined,
                },
            },
        });
    }
    return orgConfigs;
};
exports.transformEntityPermissionPoliciesToOrgFormValues = transformEntityPermissionPoliciesToOrgFormValues;
/**
 * @private
 * Transforms an array of IEntityPermissionPolicy (Hub entity) objects to an array of IHubRoleConfigValue (entity editor) objects
 * representing the channel's individual user permissions
 * @param permissionPolicies An array of IEntityPermissionPolicy objects
 * @returns an array of IHubRoleConfigValue objects
 */
const transformEntityPermissionPoliciesToUserFormValues = (permissionPolicies) => {
    return permissionPolicies.reduce((acc, permissionPolicy) => {
        return permissionPolicy.collaborationType === IEntityPermissionPolicy_1.COLLABORATION_TYPES.user
            ? [
                ...acc,
                {
                    key: permissionPolicy.collaborationId,
                    entityId: permissionPolicy.collaborationId,
                    entityType: "user",
                    roles: {
                        user: {
                            value: permissionPolicy.permission,
                            id: permissionPolicy.id,
                        },
                    },
                },
            ]
            : acc;
    }, []);
};
exports.transformEntityPermissionPoliciesToUserFormValues = transformEntityPermissionPoliciesToUserFormValues;
//# sourceMappingURL=transformEntityPermissionPoliciesToFormValues.js.map