import { COLLABORATION_TYPES, } from "../../permissions/types/IEntityPermissionPolicy";
import { CHANNEL_PERMISSIONS, ChannelNonePermission, } from "./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
 */
export const transformEntityPermissionPoliciesToPublicFormValues = (permissionPolicies) => {
    const anonymousPermission = permissionPolicies.find(({ collaborationType }) => collaborationType === COLLABORATION_TYPES.anonymous);
    const authenticatedPermission = permissionPolicies.find(({ collaborationType }) => collaborationType === COLLABORATION_TYPES.authenticated);
    return [
        {
            key: "public",
            roles: {
                anonymous: {
                    value: (anonymousPermission === null || anonymousPermission === void 0 ? void 0 : anonymousPermission.permission) || ChannelNonePermission,
                    id: anonymousPermission === null || anonymousPermission === void 0 ? void 0 : anonymousPermission.id,
                },
                authenticated: {
                    value: (authenticatedPermission === null || authenticatedPermission === void 0 ? void 0 : authenticatedPermission.permission) || ChannelNonePermission,
                    id: authenticatedPermission === null || authenticatedPermission === void 0 ? void 0 : authenticatedPermission.id,
                },
            },
        },
    ];
};
/**
 * @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
 */
export const transformEntityPermissionPoliciesToGroupFormValues = (permissionPolicies) => {
    const permissionsByGroup = permissionPolicies.reduce((acc, permissionPolicy) => {
        if ([COLLABORATION_TYPES.group, 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) || ChannelNonePermission,
                        id: group === null || group === void 0 ? void 0 : group.id,
                    },
                    admin: {
                        value: (groupAdmin === null || groupAdmin === void 0 ? void 0 : groupAdmin.permission) || ChannelNonePermission,
                        id: groupAdmin === null || groupAdmin === void 0 ? void 0 : groupAdmin.id,
                    },
                },
            },
        ];
    }, []);
};
/**
 * @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
 */
export const transformEntityPermissionPoliciesToOrgFormValues = (permissionPolicies, defaultOrgId) => {
    const permissionsByOrg = permissionPolicies.reduce((acc, permissionPolicy) => {
        if ([COLLABORATION_TYPES.org, 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) || ChannelNonePermission,
                        id: org === null || org === void 0 ? void 0 : org.id,
                    },
                    admin: {
                        value: (orgAdmin === null || orgAdmin === void 0 ? void 0 : orgAdmin.permission) || 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: ChannelNonePermission,
                    id: undefined,
                },
                admin: {
                    value: CHANNEL_PERMISSIONS.channelOwner,
                    id: undefined,
                },
            },
        });
    }
    return orgConfigs;
};
/**
 * @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
 */
export const transformEntityPermissionPoliciesToUserFormValues = (permissionPolicies) => {
    return permissionPolicies.reduce((acc, permissionPolicy) => {
        return permissionPolicy.collaborationType === COLLABORATION_TYPES.user
            ? [
                ...acc,
                {
                    key: permissionPolicy.collaborationId,
                    entityId: permissionPolicy.collaborationId,
                    entityType: "user",
                    roles: {
                        user: {
                            value: permissionPolicy.permission,
                            id: permissionPolicy.id,
                        },
                    },
                },
            ]
            : acc;
    }, []);
};
//# sourceMappingURL=transformEntityPermissionPoliciesToFormValues.js.map