import { isAssignedWorkspacePermission, isIOrganizationUser, } from "@gooddata/sdk-model";
export const sortByName = (itemA, itemB) => {
    const textA = itemA.title.toUpperCase();
    const textB = itemB.title.toUpperCase();
    return textA.localeCompare(textB);
};
export const getWorkspaceItemTestId = (workspace) => {
    return `s-workspace-item-id-${workspace.id}`;
};
export const getDataSourceItemTestId = (dataSource) => {
    return `s-data-source-item-id-${dataSource.id}`;
};
export const getUserGroupItemTestId = (userGroup) => {
    return `s-user-group-item-id-${userGroup.id}`;
};
export const getUserItemTestId = (user) => {
    return `s-user-item-id-${user.id}`;
};
const asPermissions = (permission) => {
    switch (permission) {
        case "VIEW":
            return ["VIEW"];
        case "VIEW_AND_SAVE_VIEWS":
            return ["VIEW", "CREATE_FILTER_VIEW"];
        case "VIEW_AND_EXPORT":
            return ["VIEW", "EXPORT"];
        case "VIEW_AND_EXPORT_AND_SAVE_VIEWS":
            return ["VIEW", "EXPORT", "CREATE_FILTER_VIEW"];
        case "ANALYZE":
            return ["ANALYZE"];
        case "ANALYZE_AND_EXPORT":
            return ["ANALYZE", "EXPORT"];
        case "MANAGE":
            return ["MANAGE"];
        default:
            throw Error("Unsupported permission value");
    }
};
const asGranularPermissions = (permissions) => {
    return permissions.filter(isAssignedWorkspacePermission);
};
const asDataSourcePermissions = (permission) => {
    switch (permission) {
        case "USE":
            return ["USE"];
        case "MANAGE":
            return ["MANAGE"];
        default:
            throw Error("Unsupported permission value");
    }
};
export const asDataSourcePermission = (permissions) => {
    if (permissions.includes("MANAGE")) {
        return "MANAGE";
    }
    if (permissions.includes("USE")) {
        return "USE";
    }
    return "USE";
};
export const asPermission = (permissions) => {
    if (permissions.includes("MANAGE")) {
        return "MANAGE";
    }
    if (permissions.includes("ANALYZE")) {
        return permissions.includes("EXPORT") ? "ANALYZE_AND_EXPORT" : "ANALYZE"; // TODO what about EXPORT_PDF granularity?
    }
    if (permissions.includes("VIEW")) {
        if (permissions.includes("CREATE_FILTER_VIEW")) {
            return permissions.includes("EXPORT") ? "VIEW_AND_EXPORT_AND_SAVE_VIEWS" : "VIEW_AND_SAVE_VIEWS";
        }
        return permissions.includes("EXPORT") ? "VIEW_AND_EXPORT" : "VIEW";
    }
    return "VIEW";
};
export const asEmptyDataSourcePermissionAssignment = (subjectId, subjectType, dataSource) => {
    return {
        assigneeIdentifier: {
            id: subjectId,
            type: subjectType,
        },
        dataSource: {
            id: dataSource.id,
        },
        permissions: [],
    };
};
export const asEmptyPermissionAssignment = (subjectId, subjectType, workspace) => {
    return {
        assigneeIdentifier: {
            id: subjectId,
            type: subjectType,
        },
        workspace: {
            id: workspace.id,
        },
        permissions: [],
        hierarchyPermissions: [],
    };
};
export const asPermissionAssignment = (subjectId, subjectType, workspace) => {
    const permissions = asPermissions(workspace.permissions[0]);
    return {
        assigneeIdentifier: {
            id: subjectId,
            type: subjectType,
        },
        workspace: {
            id: workspace.id,
        },
        permissions: workspace.isHierarchical ? [] : permissions,
        hierarchyPermissions: workspace.isHierarchical ? permissions : [],
    };
};
export const asDataSourcePermissionAssignment = (subjectId, subjectType, dataSource) => {
    const permissions = asDataSourcePermissions(dataSource.permission);
    return {
        assigneeIdentifier: {
            id: subjectId,
            type: subjectType,
        },
        dataSource: {
            id: dataSource.id,
        },
        permissions,
    };
};
export const extractUserName = (user) => {
    if (!user) {
        return undefined;
    }
    if (isIOrganizationUser(user)) {
        return user.fullName || user.email || user.id;
    }
    return user.fullName || user.email || user.login; // do not use ?? as for example empty email is set to ""
};
export const extractUserGroupName = (userGroup) => {
    return userGroup?.name || userGroup?.id;
};
export const grantedWorkspaceAsPermissionAssignment = (grantedWorkspace) => {
    const permissions = asGranularPermissions(grantedWorkspace.permissions);
    return {
        workspace: {
            id: grantedWorkspace.id,
        },
        permissions: grantedWorkspace.isHierarchical ? [] : permissions,
        hierarchyPermissions: grantedWorkspace.isHierarchical ? permissions : [],
    };
};
export const grantedDataSourceAsPermissionAssignment = (grantedDataSource) => {
    const permissions = asDataSourcePermissions(grantedDataSource.permission);
    return {
        dataSource: {
            id: grantedDataSource.id,
        },
        permissions,
    };
};
export const workspacePermissionsAssignmentToGrantedWorkspace = (assignment) => {
    const { workspace } = assignment;
    const assignedPermissions = assignment.hierarchyPermissions.length > 0 ? assignment.hierarchyPermissions : assignment.permissions;
    return {
        id: workspace.id,
        title: workspace.name ?? "",
        permissions: assignedPermissions,
        isHierarchical: assignment.hierarchyPermissions.length > 0,
    };
};
export const dataSourcePermissionsAssignmentToGrantedDataSource = (assignment) => {
    const { dataSource } = assignment;
    const permission = asDataSourcePermission(assignment.permissions);
    return {
        id: dataSource.id,
        title: dataSource.name ?? "",
        permission,
    };
};
//# sourceMappingURL=utils.js.map