import { VersionedStorageDomain, normalizePerPage, calculatePagination, FilesystemVersionedHelpers } from './chunk-5R5VI3A5.js';
import { deepEqual } from './chunk-ARNDQ2SQ.js';

// src/storage/domains/mcp-servers/base.ts
var MCPServersStorage = class extends VersionedStorageDomain {
  listKey = "mcpServers";
  versionMetadataFields = [
    "id",
    "mcpServerId",
    "versionNumber",
    "changedFields",
    "changeMessage",
    "createdAt"
  ];
  constructor() {
    super({
      component: "STORAGE",
      name: "MCP_SERVERS"
    });
  }
};

// src/storage/domains/mcp-servers/inmemory.ts
var InMemoryMCPServersStorage = class extends MCPServersStorage {
  db;
  constructor({ db }) {
    super();
    this.db = db;
  }
  async dangerouslyClearAll() {
    this.db.mcpServers.clear();
    this.db.mcpServerVersions.clear();
  }
  // ==========================================================================
  // MCP Server CRUD Methods
  // ==========================================================================
  async getById(id) {
    const config = this.db.mcpServers.get(id);
    return config ? this.deepCopyConfig(config) : null;
  }
  async create(input) {
    const { mcpServer } = input;
    if (this.db.mcpServers.has(mcpServer.id)) {
      throw new Error(`MCP server with id ${mcpServer.id} already exists`);
    }
    const now = /* @__PURE__ */ new Date();
    const newConfig = {
      id: mcpServer.id,
      status: "draft",
      activeVersionId: void 0,
      authorId: mcpServer.authorId,
      metadata: mcpServer.metadata,
      createdAt: now,
      updatedAt: now
    };
    this.db.mcpServers.set(mcpServer.id, newConfig);
    const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = mcpServer;
    const versionId = crypto.randomUUID();
    await this.createVersion({
      id: versionId,
      mcpServerId: mcpServer.id,
      versionNumber: 1,
      ...snapshotConfig,
      changedFields: Object.keys(snapshotConfig),
      changeMessage: "Initial version"
    });
    return this.deepCopyConfig(newConfig);
  }
  async update(input) {
    const { id, ...updates } = input;
    const existingConfig = this.db.mcpServers.get(id);
    if (!existingConfig) {
      throw new Error(`MCP server with id ${id} not found`);
    }
    const { authorId, activeVersionId, metadata, status } = updates;
    const updatedConfig = {
      ...existingConfig,
      ...authorId !== void 0 && { authorId },
      ...activeVersionId !== void 0 && { activeVersionId },
      ...status !== void 0 && { status },
      ...metadata !== void 0 && {
        metadata: { ...existingConfig.metadata, ...metadata }
      },
      updatedAt: /* @__PURE__ */ new Date()
    };
    this.db.mcpServers.set(id, updatedConfig);
    return this.deepCopyConfig(updatedConfig);
  }
  async delete(id) {
    this.db.mcpServers.delete(id);
    await this.deleteVersionsByParentId(id);
  }
  async list(args) {
    const { page = 0, perPage: perPageInput, orderBy, authorId, metadata, status = "published" } = args || {};
    const { field, direction } = this.parseOrderBy(orderBy);
    const perPage = normalizePerPage(perPageInput, 100);
    if (page < 0) {
      throw new Error("page must be >= 0");
    }
    const maxOffset = Number.MAX_SAFE_INTEGER / 2;
    if (page * perPage > maxOffset) {
      throw new Error("page value too large");
    }
    let configs = Array.from(this.db.mcpServers.values());
    if (status) {
      configs = configs.filter((config) => config.status === status);
    }
    if (authorId !== void 0) {
      configs = configs.filter((config) => config.authorId === authorId);
    }
    if (metadata && Object.keys(metadata).length > 0) {
      configs = configs.filter((config) => {
        if (!config.metadata) return false;
        return Object.entries(metadata).every(([key, value]) => deepEqual(config.metadata[key], value));
      });
    }
    const sortedConfigs = this.sortConfigs(configs, field, direction);
    const clonedConfigs = sortedConfigs.map((config) => this.deepCopyConfig(config));
    const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
    return {
      mcpServers: clonedConfigs.slice(offset, offset + perPage),
      total: clonedConfigs.length,
      page,
      perPage: perPageForResponse,
      hasMore: offset + perPage < clonedConfigs.length
    };
  }
  // ==========================================================================
  // MCP Server Version Methods
  // ==========================================================================
  async createVersion(input) {
    if (this.db.mcpServerVersions.has(input.id)) {
      throw new Error(`Version with id ${input.id} already exists`);
    }
    for (const version2 of this.db.mcpServerVersions.values()) {
      if (version2.mcpServerId === input.mcpServerId && version2.versionNumber === input.versionNumber) {
        throw new Error(`Version number ${input.versionNumber} already exists for MCP server ${input.mcpServerId}`);
      }
    }
    const version = {
      ...input,
      createdAt: /* @__PURE__ */ new Date()
    };
    this.db.mcpServerVersions.set(input.id, this.deepCopyVersion(version));
    return this.deepCopyVersion(version);
  }
  async getVersion(id) {
    const version = this.db.mcpServerVersions.get(id);
    return version ? this.deepCopyVersion(version) : null;
  }
  async getVersionByNumber(mcpServerId, versionNumber) {
    for (const version of this.db.mcpServerVersions.values()) {
      if (version.mcpServerId === mcpServerId && version.versionNumber === versionNumber) {
        return this.deepCopyVersion(version);
      }
    }
    return null;
  }
  async getLatestVersion(mcpServerId) {
    let latest = null;
    for (const version of this.db.mcpServerVersions.values()) {
      if (version.mcpServerId === mcpServerId) {
        if (!latest || version.versionNumber > latest.versionNumber) {
          latest = version;
        }
      }
    }
    return latest ? this.deepCopyVersion(latest) : null;
  }
  async listVersions(input) {
    const { mcpServerId, page = 0, perPage: perPageInput, orderBy } = input;
    const { field, direction } = this.parseVersionOrderBy(orderBy);
    const perPage = normalizePerPage(perPageInput, 20);
    if (page < 0) {
      throw new Error("page must be >= 0");
    }
    const maxOffset = Number.MAX_SAFE_INTEGER / 2;
    if (page * perPage > maxOffset) {
      throw new Error("page value too large");
    }
    let versions = Array.from(this.db.mcpServerVersions.values()).filter((v) => v.mcpServerId === mcpServerId);
    versions = this.sortVersions(versions, field, direction);
    const clonedVersions = versions.map((v) => this.deepCopyVersion(v));
    const total = clonedVersions.length;
    const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
    const paginatedVersions = clonedVersions.slice(offset, offset + perPage);
    return {
      versions: paginatedVersions,
      total,
      page,
      perPage: perPageForResponse,
      hasMore: offset + perPage < total
    };
  }
  async deleteVersion(id) {
    this.db.mcpServerVersions.delete(id);
  }
  async deleteVersionsByParentId(entityId) {
    const idsToDelete = [];
    for (const [id, version] of this.db.mcpServerVersions.entries()) {
      if (version.mcpServerId === entityId) {
        idsToDelete.push(id);
      }
    }
    for (const id of idsToDelete) {
      this.db.mcpServerVersions.delete(id);
    }
  }
  async countVersions(mcpServerId) {
    let count = 0;
    for (const version of this.db.mcpServerVersions.values()) {
      if (version.mcpServerId === mcpServerId) {
        count++;
      }
    }
    return count;
  }
  // ==========================================================================
  // Private Helper Methods
  // ==========================================================================
  deepCopyConfig(config) {
    return {
      ...config,
      metadata: config.metadata ? { ...config.metadata } : config.metadata
    };
  }
  deepCopyVersion(version) {
    return {
      ...version,
      tools: version.tools ? JSON.parse(JSON.stringify(version.tools)) : version.tools,
      agents: version.agents ? JSON.parse(JSON.stringify(version.agents)) : version.agents,
      workflows: version.workflows ? JSON.parse(JSON.stringify(version.workflows)) : version.workflows,
      repository: version.repository ? { ...version.repository } : version.repository,
      changedFields: version.changedFields ? [...version.changedFields] : version.changedFields
    };
  }
  sortConfigs(configs, field, direction) {
    return configs.sort((a, b) => {
      const aValue = a[field].getTime();
      const bValue = b[field].getTime();
      return direction === "ASC" ? aValue - bValue : bValue - aValue;
    });
  }
  sortVersions(versions, field, direction) {
    return versions.sort((a, b) => {
      let aVal;
      let bVal;
      if (field === "createdAt") {
        aVal = a.createdAt.getTime();
        bVal = b.createdAt.getTime();
      } else {
        aVal = a.versionNumber;
        bVal = b.versionNumber;
      }
      return direction === "ASC" ? aVal - bVal : bVal - aVal;
    });
  }
};

// src/storage/domains/mcp-servers/filesystem.ts
var FilesystemMCPServersStorage = class extends MCPServersStorage {
  helpers;
  constructor({ db }) {
    super();
    this.helpers = new FilesystemVersionedHelpers({
      db,
      entitiesFile: "mcp-servers.json",
      parentIdField: "mcpServerId",
      name: "FilesystemMCPServersStorage",
      versionMetadataFields: ["id", "mcpServerId", "versionNumber", "changedFields", "changeMessage", "createdAt"]
    });
  }
  async init() {
    await this.helpers.db.init();
  }
  async dangerouslyClearAll() {
    await this.helpers.dangerouslyClearAll();
  }
  async getById(id) {
    return this.helpers.getById(id);
  }
  async create(input) {
    const { mcpServer } = input;
    const now = /* @__PURE__ */ new Date();
    const entity = {
      id: mcpServer.id,
      status: "draft",
      activeVersionId: void 0,
      authorId: mcpServer.authorId,
      metadata: mcpServer.metadata,
      createdAt: now,
      updatedAt: now
    };
    await this.helpers.createEntity(mcpServer.id, entity);
    const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = mcpServer;
    const versionId = crypto.randomUUID();
    await this.createVersion({
      id: versionId,
      mcpServerId: mcpServer.id,
      versionNumber: 1,
      ...snapshotConfig,
      changedFields: Object.keys(snapshotConfig),
      changeMessage: "Initial version"
    });
    return structuredClone(entity);
  }
  async update(input) {
    const { id, ...updates } = input;
    return this.helpers.updateEntity(id, updates);
  }
  async delete(id) {
    await this.helpers.deleteEntity(id);
  }
  async list(args) {
    const { page, perPage, orderBy, authorId, metadata, status } = args || {};
    const result = await this.helpers.listEntities({
      page,
      perPage,
      orderBy,
      listKey: "mcpServers",
      filters: { authorId, metadata, status }
    });
    return result;
  }
  async createVersion(input) {
    return this.helpers.createVersion(input);
  }
  async getVersion(id) {
    return this.helpers.getVersion(id);
  }
  async getVersionByNumber(mcpServerId, versionNumber) {
    return this.helpers.getVersionByNumber(mcpServerId, versionNumber);
  }
  async getLatestVersion(mcpServerId) {
    return this.helpers.getLatestVersion(mcpServerId);
  }
  async listVersions(input) {
    const result = await this.helpers.listVersions(input, "mcpServerId");
    return result;
  }
  async deleteVersion(id) {
    await this.helpers.deleteVersion(id);
  }
  async deleteVersionsByParentId(entityId) {
    await this.helpers.deleteVersionsByParentId(entityId);
  }
  async countVersions(mcpServerId) {
    return this.helpers.countVersions(mcpServerId);
  }
};

export { FilesystemMCPServersStorage, InMemoryMCPServersStorage, MCPServersStorage };
//# sourceMappingURL=chunk-ZLF3OVXU.js.map
//# sourceMappingURL=chunk-ZLF3OVXU.js.map