"use strict";
/**
 * PermissionRegistry
 *
 * Central registry for managing app permissions at runtime.
 * Supports glob patterns for permission matching:
 * - "fs/read" matches exactly "fs/read"
 * - "fs/*" matches any permission starting with "fs/"
 * - "*" matches all permissions
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PermissionRegistry = void 0;
exports.registerEventPermission = registerEventPermission;
exports.getEventPermission = getEventPermission;
exports.getAllEventPermissions = getAllEventPermissions;
const tsyringe_1 = require("tsyringe");
let PermissionRegistry = class PermissionRegistry {
    appPermissions = new Map();
    /**
     * Register permissions for an app (called during install/load)
     */
    registerApp(appId, permissions) {
        this.appPermissions.set(appId, new Set(permissions));
        console.log(`[PermissionRegistry] Registered ${permissions.length} permissions for ${appId}`);
    }
    /**
     * Unregister app permissions (called during uninstall)
     */
    unregisterApp(appId) {
        this.appPermissions.delete(appId);
        console.log(`[PermissionRegistry] Unregistered permissions for ${appId}`);
    }
    /**
     * Check if an app has a specific permission.
     * Supports glob patterns in the app's granted permissions.
     */
    hasPermission(appId, requiredPermission) {
        const permissions = this.appPermissions.get(appId);
        if (!permissions) {
            return false;
        }
        // Check for exact match first
        if (permissions.has(requiredPermission)) {
            return true;
        }
        // Check for glob patterns
        for (const granted of permissions) {
            if (this.matchesGlob(granted, requiredPermission)) {
                return true;
            }
        }
        return false;
    }
    /**
     * Match a glob pattern against a permission.
     * - "*" matches everything
     * - "namespace/*" matches anything starting with "namespace/"
     */
    matchesGlob(pattern, permission) {
        // Wildcard matches everything
        if (pattern === "*") {
            return true;
        }
        // Namespace wildcard: "fs/*" matches "fs/read", "fs/write", etc.
        if (pattern.endsWith("/*")) {
            const namespace = pattern.slice(0, -2); // Remove "/*"
            return permission.startsWith(namespace + "/");
        }
        return false;
    }
    /**
     * Get all permissions for an app
     */
    getPermissions(appId) {
        const permissions = this.appPermissions.get(appId);
        return permissions ? Array.from(permissions) : [];
    }
    /**
     * Check if an app is registered
     */
    hasApp(appId) {
        return this.appPermissions.has(appId);
    }
};
exports.PermissionRegistry = PermissionRegistry;
exports.PermissionRegistry = PermissionRegistry = __decorate([
    (0, tsyringe_1.singleton)(),
    (0, tsyringe_1.injectable)()
], PermissionRegistry);
/**
 * Event Permission Registry
 *
 * Stores which permissions are required to subscribe to specific events.
 */
const EVENT_PERMISSIONS = new Map();
/**
 * Register an event's required permission
 */
function registerEventPermission(eventName, permission) {
    EVENT_PERMISSIONS.set(eventName, permission);
}
/**
 * Get the permission required to subscribe to an event
 */
function getEventPermission(eventName) {
    return EVENT_PERMISSIONS.get(eventName);
}
/**
 * Get all registered event permissions
 */
function getAllEventPermissions() {
    return new Map(EVENT_PERMISSIONS);
}
//# sourceMappingURL=PermissionRegistry.js.map