class DefaultRouteRegistry {
    routes = new Map();
    register(route) {
        if (this.routes.has(route.path)) {
            throw new Error(`Route already registered: ${route.path}`);
        }
        this.routes.set(route.path, route);
    }
    list() {
        return Array.from(this.routes.values());
    }
}
class DefaultMiddlewareRegistry {
    middleware = [];
    register(middleware) {
        this.middleware.push(middleware);
    }
    list() {
        return [...this.middleware];
    }
}
export function createExtensionManager(models, logger) {
    const context = {
        models,
        routes: new DefaultRouteRegistry(),
        middleware: new DefaultMiddlewareRegistry(),
        logger,
    };
    return {
        context,
        async load(extension) {
            if (extension.manifest !== undefined) {
                validateManifest(extension.name, extension.manifest);
            }
            logger.info("loading trusted local extension", {
                extension: extension.name,
                apiVersion: extension.manifest?.apiVersion ?? "implicit-local",
                trusted: extension.manifest?.trusted ?? "local",
                permissions: extension.manifest?.permissions ?? ["legacy-full-local"],
            });
            await extension.setup(context);
        },
    };
}
function validateManifest(name, manifest) {
    if (manifest.apiVersion !== "cursor-rpc/v1") {
        throw new Error(`Extension ${name} declares unsupported apiVersion`);
    }
    if (manifest.trusted !== "local") {
        throw new Error(`Extension ${name} must declare trusted=local`);
    }
}
