'use strict';

var chunkJ3SEZ24R_cjs = require('./chunk-J3SEZ24R.cjs');
var chunk2XZ2466F_cjs = require('./chunk-2XZ2466F.cjs');
var chunkY2BKW5ND_cjs = require('./chunk-Y2BKW5ND.cjs');
var chunk6P7P7Y5U_cjs = require('./chunk-6P7P7Y5U.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');

// src/server/handlers/mcp-client-versions.ts
var LIST_MCP_CLIENT_VERSIONS_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients/:mcpClientId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionPathParams,
  queryParamSchema: chunkJ3SEZ24R_cjs.listMCPClientVersionsQuerySchema,
  responseSchema: chunkJ3SEZ24R_cjs.listMCPClientVersionsResponseSchema,
  summary: "List MCP client versions",
  description: "Returns a paginated list of all versions for a stored MCP client",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, page, perPage, orderBy }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const mcpClient = await mcpClientStore.getById(mcpClientId);
      if (!mcpClient) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP client with id ${mcpClientId} not found` });
      }
      const result = await mcpClientStore.listVersions({
        mcpClientId,
        page,
        perPage,
        orderBy
      });
      return result;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error listing MCP client versions");
    }
  }
});
var CREATE_MCP_CLIENT_VERSION_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "POST",
  path: "/stored/mcp-clients/:mcpClientId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionPathParams,
  bodySchema: chunkJ3SEZ24R_cjs.createMCPClientVersionBodySchema,
  responseSchema: chunkJ3SEZ24R_cjs.createMCPClientVersionResponseSchema,
  summary: "Create MCP client version",
  description: "Creates a new version snapshot of the current MCP client configuration",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, changeMessage }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const mcpClient = await mcpClientStore.getById(mcpClientId);
      if (!mcpClient) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP client with id ${mcpClientId} not found` });
      }
      let currentConfig = {};
      if (mcpClient.activeVersionId) {
        const activeVersion = await mcpClientStore.getVersion(mcpClient.activeVersionId);
        if (activeVersion) {
          currentConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
            activeVersion,
            chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
          );
        }
      }
      const latestVersion = await mcpClientStore.getLatestVersion(mcpClientId);
      if (!mcpClient.activeVersionId && latestVersion) {
        currentConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
          latestVersion,
          chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
        );
      }
      const previousConfig = latestVersion ? chunk2XZ2466F_cjs.extractConfigFromVersion(
        latestVersion,
        chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
      ) : null;
      const changedFields = chunk2XZ2466F_cjs.calculateChangedFields(previousConfig, currentConfig);
      const { versionId } = await chunk2XZ2466F_cjs.createVersionWithRetry(
        mcpClientStore,
        mcpClientId,
        "mcpClientId",
        currentConfig,
        changedFields,
        { changeMessage }
      );
      const version = await mcpClientStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await chunk2XZ2466F_cjs.enforceRetentionLimit(
        mcpClientStore,
        mcpClientId,
        "mcpClientId",
        mcpClient.activeVersionId
      );
      return version;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error creating MCP client version");
    }
  }
});
var GET_MCP_CLIENT_VERSION_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients/:mcpClientId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionIdPathParams,
  responseSchema: chunkJ3SEZ24R_cjs.getMCPClientVersionResponseSchema,
  summary: "Get MCP client version",
  description: "Returns a specific version of an MCP client by its version ID",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, versionId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const version = await mcpClientStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.mcpClientId !== mcpClientId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for MCP client ${mcpClientId}`
        });
      }
      return version;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error getting MCP client version");
    }
  }
});
var ACTIVATE_MCP_CLIENT_VERSION_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "POST",
  path: "/stored/mcp-clients/:mcpClientId/versions/:versionId/activate",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionIdPathParams,
  responseSchema: chunkJ3SEZ24R_cjs.activateMCPClientVersionResponseSchema,
  summary: "Activate MCP client version",
  description: "Sets a specific version as the active version for the MCP client",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, versionId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const mcpClient = await mcpClientStore.getById(mcpClientId);
      if (!mcpClient) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP client with id ${mcpClientId} not found` });
      }
      const version = await mcpClientStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.mcpClientId !== mcpClientId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for MCP client ${mcpClientId}`
        });
      }
      await mcpClientStore.update({
        id: mcpClientId,
        activeVersionId: versionId,
        status: "published"
      });
      mastra.getEditor()?.mcp.clearCache(mcpClientId);
      return {
        success: true,
        message: `Version ${version.versionNumber} is now active`,
        activeVersionId: versionId
      };
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error activating MCP client version");
    }
  }
});
var RESTORE_MCP_CLIENT_VERSION_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "POST",
  path: "/stored/mcp-clients/:mcpClientId/versions/:versionId/restore",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionIdPathParams,
  responseSchema: chunkJ3SEZ24R_cjs.restoreMCPClientVersionResponseSchema,
  summary: "Restore MCP client version",
  description: "Restores the MCP client configuration from a version, creating a new version",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, versionId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const mcpClient = await mcpClientStore.getById(mcpClientId);
      if (!mcpClient) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP client with id ${mcpClientId} not found` });
      }
      const versionToRestore = await mcpClientStore.getVersion(versionId);
      if (!versionToRestore) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (versionToRestore.mcpClientId !== mcpClientId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for MCP client ${mcpClientId}`
        });
      }
      const restoredConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        versionToRestore,
        chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
      );
      await mcpClientStore.update({
        id: mcpClientId,
        ...restoredConfig
      });
      const latestVersion = await mcpClientStore.getLatestVersion(mcpClientId);
      const previousConfig = latestVersion ? chunk2XZ2466F_cjs.extractConfigFromVersion(
        latestVersion,
        chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
      ) : null;
      const changedFields = chunk2XZ2466F_cjs.calculateChangedFields(previousConfig, restoredConfig);
      const { versionId: newVersionId } = await chunk2XZ2466F_cjs.createVersionWithRetry(
        mcpClientStore,
        mcpClientId,
        "mcpClientId",
        restoredConfig,
        changedFields,
        {
          changeMessage: `Restored from version ${versionToRestore.versionNumber}`
        }
      );
      const newVersion = await mcpClientStore.getVersion(newVersionId);
      if (!newVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await chunk2XZ2466F_cjs.enforceRetentionLimit(
        mcpClientStore,
        mcpClientId,
        "mcpClientId",
        mcpClient.activeVersionId
      );
      mastra.getEditor()?.mcp.clearCache(mcpClientId);
      return newVersion;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error restoring MCP client version");
    }
  }
});
var DELETE_MCP_CLIENT_VERSION_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "DELETE",
  path: "/stored/mcp-clients/:mcpClientId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionIdPathParams,
  responseSchema: chunkJ3SEZ24R_cjs.deleteMCPClientVersionResponseSchema,
  summary: "Delete MCP client version",
  description: "Deletes a specific version (cannot delete the active version)",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, versionId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const mcpClient = await mcpClientStore.getById(mcpClientId);
      if (!mcpClient) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `MCP client with id ${mcpClientId} not found` });
      }
      const version = await mcpClientStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.mcpClientId !== mcpClientId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for MCP client ${mcpClientId}`
        });
      }
      if (mcpClient.activeVersionId === versionId) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Cannot delete the active version. Activate a different version first."
        });
      }
      await mcpClientStore.deleteVersion(versionId);
      mastra.getEditor()?.mcp.clearCache(mcpClientId);
      return {
        success: true,
        message: `Version ${version.versionNumber} deleted successfully`
      };
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error deleting MCP client version");
    }
  }
});
var COMPARE_MCP_CLIENT_VERSIONS_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/mcp-clients/:mcpClientId/versions/compare",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkJ3SEZ24R_cjs.mcpClientVersionPathParams,
  queryParamSchema: chunkJ3SEZ24R_cjs.compareMCPClientVersionsQuerySchema,
  responseSchema: chunkJ3SEZ24R_cjs.compareMCPClientVersionsResponseSchema,
  summary: "Compare MCP client versions",
  description: "Compares two versions and returns the differences between them",
  tags: ["MCP Client Versions"],
  handler: async ({ mastra, mcpClientId, from, to }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const mcpClientStore = await storage.getStore("mcpClients");
      if (!mcpClientStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "MCP clients storage domain is not available" });
      }
      const fromVersion = await mcpClientStore.getVersion(from);
      if (!fromVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${from} not found` });
      }
      if (fromVersion.mcpClientId !== mcpClientId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${from} not found for MCP client ${mcpClientId}`
        });
      }
      const toVersion = await mcpClientStore.getVersion(to);
      if (!toVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${to} not found` });
      }
      if (toVersion.mcpClientId !== mcpClientId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${to} not found for MCP client ${mcpClientId}`
        });
      }
      const fromConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        fromVersion,
        chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
      );
      const toConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        toVersion,
        chunk2XZ2466F_cjs.MCP_CLIENT_SNAPSHOT_CONFIG_FIELDS
      );
      const diffs = chunk2XZ2466F_cjs.computeVersionDiffs(fromConfig, toConfig);
      return {
        diffs,
        fromVersion,
        toVersion
      };
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error comparing MCP client versions");
    }
  }
});

exports.ACTIVATE_MCP_CLIENT_VERSION_ROUTE = ACTIVATE_MCP_CLIENT_VERSION_ROUTE;
exports.COMPARE_MCP_CLIENT_VERSIONS_ROUTE = COMPARE_MCP_CLIENT_VERSIONS_ROUTE;
exports.CREATE_MCP_CLIENT_VERSION_ROUTE = CREATE_MCP_CLIENT_VERSION_ROUTE;
exports.DELETE_MCP_CLIENT_VERSION_ROUTE = DELETE_MCP_CLIENT_VERSION_ROUTE;
exports.GET_MCP_CLIENT_VERSION_ROUTE = GET_MCP_CLIENT_VERSION_ROUTE;
exports.LIST_MCP_CLIENT_VERSIONS_ROUTE = LIST_MCP_CLIENT_VERSIONS_ROUTE;
exports.RESTORE_MCP_CLIENT_VERSION_ROUTE = RESTORE_MCP_CLIENT_VERSION_ROUTE;
//# sourceMappingURL=chunk-DMAGP2MP.cjs.map
//# sourceMappingURL=chunk-DMAGP2MP.cjs.map