import { zodToJsonSchema } from "zod-to-json-schema";
import { getToolRegistry } from "./tool-registry.js";
import { executeTool } from "./tool-executor.js";
import { loadAllModuleTools } from "./tool-loader.js";
import { authenticateMcpRequest, hasRequiredFeatures } from "./auth.js";
class InProcessMcpClient {
  constructor(auth, container) {
    this.toolsLoaded = false;
    this.auth = auth;
    this.container = container;
    this.toolContext = {
      tenantId: auth.tenantId,
      organizationId: auth.organizationId,
      userId: auth.userId,
      container,
      userFeatures: auth.features,
      isSuperAdmin: auth.isSuperAdmin
    };
  }
  /**
   * Create and authenticate an in-process client using API key.
   */
  static async create(options) {
    const { apiKeySecret, container } = options;
    const authResult = await authenticateMcpRequest(apiKeySecret, container);
    if (!authResult.success) {
      throw new Error(`Authentication failed: ${authResult.error}`);
    }
    return new InProcessMcpClient(authResult, container);
  }
  /**
   * Create an in-process client with pre-authenticated context.
   * Use this when you already have user auth context (e.g., from session auth).
   */
  static async createWithAuthContext(options) {
    const { container, authContext } = options;
    const syntheticAuth = {
      success: true,
      keyId: "session-auth",
      keyName: "Session Authentication",
      tenantId: authContext.tenantId,
      organizationId: authContext.organizationId,
      userId: authContext.userId,
      features: authContext.userFeatures,
      isSuperAdmin: authContext.isSuperAdmin
    };
    return new InProcessMcpClient(syntheticAuth, container);
  }
  /**
   * Ensure tools are loaded from all modules.
   */
  async ensureToolsLoaded() {
    if (!this.toolsLoaded) {
      await loadAllModuleTools();
      this.toolsLoaded = true;
    }
  }
  /**
   * List available tools filtered by API key's permissions.
   * Returns JSON Schema format (for MCP protocol compatibility).
   */
  async listTools() {
    await this.ensureToolsLoaded();
    const registry = getToolRegistry();
    const tools = Array.from(registry.getTools().values());
    const rbacService = this.container.resolve("rbacService");
    const accessibleTools = tools.filter(
      (tool) => hasRequiredFeatures(tool.requiredFeatures, this.auth.features, this.auth.isSuperAdmin, rbacService)
    );
    return accessibleTools.map((tool) => ({
      name: tool.name,
      description: tool.description,
      inputSchema: zodToJsonSchema(tool.inputSchema)
    }));
  }
  /**
   * List available tools with raw Zod schemas.
   * Use this for AI SDK integration which requires Zod schemas.
   */
  async listToolsWithSchemas() {
    await this.ensureToolsLoaded();
    const registry = getToolRegistry();
    const tools = Array.from(registry.getTools().values());
    const rbacService = this.container.resolve("rbacService");
    const accessibleTools = tools.filter(
      (tool) => hasRequiredFeatures(tool.requiredFeatures, this.auth.features, this.auth.isSuperAdmin, rbacService)
    );
    return accessibleTools.map((tool) => ({
      name: tool.name,
      description: tool.description,
      inputSchema: tool.inputSchema
    }));
  }
  /**
   * Execute a tool directly.
   */
  async callTool(name, args) {
    await this.ensureToolsLoaded();
    const result = await executeTool(name, args ?? {}, this.toolContext);
    return {
      success: result.success,
      result: result.result,
      error: result.error
    };
  }
  /**
   * Close the client (no-op for in-process).
   */
  async close() {
  }
  /**
   * Get the authenticated context info.
   */
  getAuthInfo() {
    return {
      keyId: this.auth.keyId,
      keyName: this.auth.keyName,
      tenantId: this.auth.tenantId,
      organizationId: this.auth.organizationId,
      userId: this.auth.userId,
      isSuperAdmin: this.auth.isSuperAdmin
    };
  }
}
export {
  InProcessMcpClient
};
//# sourceMappingURL=in-process-client.js.map
