"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSystemRole = exports.getSystemRoles = exports.getNonEnterpriseScopesForRole = exports.getAllScopesForRole = exports.PROJECT_ROLE_TO_SCOPES_MAP = void 0;
const projectMemberRole_1 = require("../types/projectMemberRole");
/**
 * Utility functions to convert project member roles to equivalent scope sets
 * for testing migration compatibility between role-based and scope-based authorization
 */
/**
 * Base scopes for each role level (without inheritance)
 */
const BASE_ROLE_SCOPES = {
    [projectMemberRole_1.ProjectMemberRole.VIEWER]: [
        // Basic viewing permissions
        'view:Dashboard',
        'view:JobStatus@self', // For viewing job status created by user
        'view:SavedChart',
        'view:Space',
        'view:Project',
        'view:PinnedItems',
        'view:DashboardComments',
        'view:Tags',
        'manage:ExportCsv',
        // Enterprise scopes (when available)
        'view:MetricsTree',
        'view:SpotlightTableConfig',
        'view:AiAgentThread@self',
    ],
    [projectMemberRole_1.ProjectMemberRole.INTERACTIVE_VIEWER]: [
        // Additional interactive viewer permissions
        'view:UnderlyingData',
        'view:SemanticViewer',
        'manage:Explore',
        'manage:ChangeCsvResults',
        'create:ScheduledDeliveries',
        'manage:ScheduledDeliveries@self',
        'create:DashboardComments',
        'manage:GoogleSheets',
        // Space-level content management (requires space admin/editor role)
        'manage:Dashboard@space', // Via space access
        'manage:SavedChart@space', // Via space access
        'manage:Space@assigned', // Via space access (admin role)
        // Enterprise scopes
        'view:AiAgent',
        'create:AiAgentThread',
    ],
    [projectMemberRole_1.ProjectMemberRole.EDITOR]: [
        // Editor-specific permissions
        'create:Space',
        'manage:Space@public', // For non-private spaces
        'manage:Job',
        'manage:PinnedItems',
        'manage:DashboardComments',
        'manage:Tags',
        // Enterprise scopes
        'manage:MetricsTree',
        'manage:AiAgentThread@self', // User's own threads
    ],
    [projectMemberRole_1.ProjectMemberRole.DEVELOPER]: [
        // Developer-specific permissions
        'manage:VirtualView',
        'manage:CustomSql',
        'manage:SqlRunner',
        'manage:Validation',
        'manage:CompileProject',
        'create:Project@preview', // Preview projects
        'delete:Project@self', // Preview projects created by user
        'update:Project',
        'update:Project@self',
        'view:JobStatus', // All jobs in project
        'view:SourceCode',
        'manage:SourceCode',
        // Enterprise scopes
        'manage:SpotlightTableConfig',
        'manage:ContentAsCode',
        'manage:AiAgent',
        'manage:AiAgentThread@self', // User's own threads
    ],
    [projectMemberRole_1.ProjectMemberRole.ADMIN]: [
        // Admin-specific permissions
        'delete:Project', // Any project
        'view:Analytics',
        'manage:Dashboard', // All dashboards
        'manage:Space', // All spaces
        'manage:Project', // Required for managing non-private spaces
        'manage:SavedChart', // All saved charts
        'view:AiAgentThread', // All threads in project
        'manage:AiAgentThread', // All threads in project
        'manage:ScheduledDeliveries',
    ],
};
/**
 * Role hierarchy for inheritance
 */
const ROLE_HIERARCHY = [
    projectMemberRole_1.ProjectMemberRole.VIEWER,
    projectMemberRole_1.ProjectMemberRole.INTERACTIVE_VIEWER,
    projectMemberRole_1.ProjectMemberRole.EDITOR,
    projectMemberRole_1.ProjectMemberRole.DEVELOPER,
    projectMemberRole_1.ProjectMemberRole.ADMIN,
];
/**
 * Maps project member roles to their equivalent scopes based on projectMemberAbility.ts analysis
 * Each role inherits permissions from the roles below it in the hierarchy
 */
exports.PROJECT_ROLE_TO_SCOPES_MAP = (() => {
    const result = {};
    for (const role of ROLE_HIERARCHY) {
        const roleIndex = ROLE_HIERARCHY.indexOf(role);
        const inheritedScopes = new Set();
        // Add scopes from all lower-level roles
        for (let i = 0; i <= roleIndex; i += 1) {
            const currentRole = ROLE_HIERARCHY[i];
            BASE_ROLE_SCOPES[currentRole].forEach((scope) => inheritedScopes.add(scope));
        }
        result[role] = Array.from(inheritedScopes);
    }
    return result;
})();
/**
 * Gets the scopes required for a specific project member role
 */
const getAllScopesForRole = (role) => [
    ...exports.PROJECT_ROLE_TO_SCOPES_MAP[role],
];
exports.getAllScopesForRole = getAllScopesForRole;
/**
 * Gets only the non-enterprise scopes for a role (filters out enterprise-only features)
 */
const getNonEnterpriseScopesForRole = (role) => {
    const enterpriseScopes = new Set([
        'view:MetricsTree',
        'manage:MetricsTree',
        'view:SpotlightTableConfig',
        'manage:SpotlightTableConfig',
        'view:AiAgent',
        'view:AiAgentThread',
        'create:AiAgentThread',
        'manage:AiAgent',
        'manage:AiAgentThread',
        'manage:ContentAsCode',
        'manage:PersonalAccessToken',
    ]);
    return exports.PROJECT_ROLE_TO_SCOPES_MAP[role].filter((scope) => !enterpriseScopes.has(scope));
};
exports.getNonEnterpriseScopesForRole = getNonEnterpriseScopesForRole;
const getSystemRoles = () => ROLE_HIERARCHY.map((role) => ({
    roleUuid: role,
    name: projectMemberRole_1.ProjectMemberRoleLabels[role],
    description: projectMemberRole_1.ProjectMemberRoleLabels[role],
    ownerType: 'system',
    scopes: (0, exports.getAllScopesForRole)(role),
    organizationUuid: null,
    createdAt: null,
    updatedAt: null,
    createdBy: null,
}));
exports.getSystemRoles = getSystemRoles;
const isSystemRole = (roleUuid) => ROLE_HIERARCHY.includes(roleUuid);
exports.isSystemRole = isSystemRole;
//# sourceMappingURL=roleToScopeMapping.js.map