"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertProjectRoleToOrganizationRole = exports.convertProjectRoleToSpaceRole = exports.getHighestSpaceRole = exports.getHighestProjectRole = exports.convertSpaceRoleToProjectRole = exports.convertOrganizationRoleToProjectRole = void 0;
const tslib_1 = require("tslib");
const organizationMemberProfile_1 = require("../types/organizationMemberProfile");
const projectMemberRole_1 = require("../types/projectMemberRole");
const space_1 = require("../types/space");
const assertUnreachable_1 = tslib_1.__importDefault(require("./assertUnreachable"));
const convertOrganizationRoleToProjectRole = (organizationRole) => {
    switch (organizationRole) {
        case organizationMemberProfile_1.OrganizationMemberRole.VIEWER:
            return projectMemberRole_1.ProjectMemberRole.VIEWER;
        case organizationMemberProfile_1.OrganizationMemberRole.INTERACTIVE_VIEWER:
            return projectMemberRole_1.ProjectMemberRole.INTERACTIVE_VIEWER;
        case organizationMemberProfile_1.OrganizationMemberRole.EDITOR:
            return projectMemberRole_1.ProjectMemberRole.EDITOR;
        case organizationMemberProfile_1.OrganizationMemberRole.DEVELOPER:
            return projectMemberRole_1.ProjectMemberRole.DEVELOPER;
        case organizationMemberProfile_1.OrganizationMemberRole.ADMIN:
            return projectMemberRole_1.ProjectMemberRole.ADMIN;
        case organizationMemberProfile_1.OrganizationMemberRole.MEMBER:
            return undefined;
        default:
            return (0, assertUnreachable_1.default)(organizationRole, `Unknown role ${organizationRole}`);
    }
};
exports.convertOrganizationRoleToProjectRole = convertOrganizationRoleToProjectRole;
const convertSpaceRoleToProjectRole = (spaceRole) => {
    switch (spaceRole) {
        case space_1.SpaceMemberRole.VIEWER:
            return projectMemberRole_1.ProjectMemberRole.VIEWER;
        case space_1.SpaceMemberRole.EDITOR:
            return projectMemberRole_1.ProjectMemberRole.EDITOR;
        case space_1.SpaceMemberRole.ADMIN:
            return projectMemberRole_1.ProjectMemberRole.ADMIN;
        default:
            return (0, assertUnreachable_1.default)(spaceRole, `Unknown space role ${spaceRole}`);
    }
};
exports.convertSpaceRoleToProjectRole = convertSpaceRoleToProjectRole;
const getHighestProjectRole = (inheritedRoles) => inheritedRoles.reduce((highestRole, role) => {
    if (role.role === undefined) {
        return highestRole;
    }
    if (highestRole?.role === undefined ||
        projectMemberRole_1.ProjectRoleOrder[role.role] >=
            projectMemberRole_1.ProjectRoleOrder[highestRole.role]) {
        return {
            type: role.type,
            role: role.role,
        };
    }
    return highestRole;
}, undefined);
exports.getHighestProjectRole = getHighestProjectRole;
const getHighestSpaceRole = (spaceRoles) => spaceRoles
    .filter((role) => role !== undefined)
    .sort((a, b) => projectMemberRole_1.SpaceRoleOrder[a] - projectMemberRole_1.SpaceRoleOrder[b])
    .reverse()[0];
exports.getHighestSpaceRole = getHighestSpaceRole;
const convertProjectRoleToSpaceRole = (projectRole) => {
    switch (projectRole) {
        case projectMemberRole_1.ProjectMemberRole.VIEWER:
            return space_1.SpaceMemberRole.VIEWER;
        case projectMemberRole_1.ProjectMemberRole.INTERACTIVE_VIEWER:
            return space_1.SpaceMemberRole.VIEWER;
        case projectMemberRole_1.ProjectMemberRole.EDITOR:
            return space_1.SpaceMemberRole.EDITOR;
        case projectMemberRole_1.ProjectMemberRole.DEVELOPER:
            return space_1.SpaceMemberRole.EDITOR;
        case projectMemberRole_1.ProjectMemberRole.ADMIN:
            return space_1.SpaceMemberRole.ADMIN;
        default:
            return (0, assertUnreachable_1.default)(projectRole, `Project role ${projectRole} does not match Space roles`);
    }
};
exports.convertProjectRoleToSpaceRole = convertProjectRoleToSpaceRole;
const convertProjectRoleToOrganizationRole = (projectRole) => {
    switch (projectRole) {
        case projectMemberRole_1.ProjectMemberRole.VIEWER:
            return organizationMemberProfile_1.OrganizationMemberRole.VIEWER;
        case projectMemberRole_1.ProjectMemberRole.INTERACTIVE_VIEWER:
            return organizationMemberProfile_1.OrganizationMemberRole.INTERACTIVE_VIEWER;
        case projectMemberRole_1.ProjectMemberRole.EDITOR:
            return organizationMemberProfile_1.OrganizationMemberRole.EDITOR;
        case projectMemberRole_1.ProjectMemberRole.DEVELOPER:
            return organizationMemberProfile_1.OrganizationMemberRole.DEVELOPER;
        case projectMemberRole_1.ProjectMemberRole.ADMIN:
            return organizationMemberProfile_1.OrganizationMemberRole.ADMIN;
        default:
            return (0, assertUnreachable_1.default)(projectRole, `Project role ${projectRole} does not match Organization roles`);
    }
};
exports.convertProjectRoleToOrganizationRole = convertProjectRoleToOrganizationRole;
//# sourceMappingURL=projectMemberRole.js.map