const MCP_SERVERS_CONFIG_KEY = "mcp_servers";
async function getMcpServerConfigs(resolver) {
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    return [];
  }
  try {
    const value = await service.getValue(
      "ai_assistant",
      MCP_SERVERS_CONFIG_KEY,
      { defaultValue: [] }
    );
    return value ?? [];
  } catch {
    return [];
  }
}
async function getMcpServerConfig(resolver, serverId) {
  const configs = await getMcpServerConfigs(resolver);
  return configs.find((c) => c.id === serverId) ?? null;
}
async function getEnabledMcpServerConfigs(resolver) {
  const configs = await getMcpServerConfigs(resolver);
  return configs.filter((c) => c.enabled);
}
async function saveMcpServerConfig(resolver, config) {
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    throw new Error("Configuration service unavailable");
  }
  const configs = await getMcpServerConfigs(resolver);
  const now = (/* @__PURE__ */ new Date()).toISOString();
  let updatedConfigs;
  let savedConfig;
  if (config.id) {
    const existingIndex = configs.findIndex((c) => c.id === config.id);
    if (existingIndex === -1) {
      throw new Error(`MCP server config not found: ${config.id}`);
    }
    savedConfig = {
      ...configs[existingIndex],
      ...config,
      id: config.id,
      updatedAt: now
    };
    updatedConfigs = [
      ...configs.slice(0, existingIndex),
      savedConfig,
      ...configs.slice(existingIndex + 1)
    ];
  } else {
    savedConfig = {
      ...config,
      id: generateId(),
      createdAt: now,
      updatedAt: now
    };
    updatedConfigs = [...configs, savedConfig];
  }
  await service.setValue("ai_assistant", MCP_SERVERS_CONFIG_KEY, updatedConfigs);
  return savedConfig;
}
async function updateMcpServerConfig(resolver, serverId, updates) {
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    throw new Error("Configuration service unavailable");
  }
  const configs = await getMcpServerConfigs(resolver);
  const existingIndex = configs.findIndex((c) => c.id === serverId);
  if (existingIndex === -1) {
    throw new Error(`MCP server config not found: ${serverId}`);
  }
  const updatedConfig = {
    ...configs[existingIndex],
    ...updates,
    id: serverId,
    // Ensure ID is preserved
    updatedAt: (/* @__PURE__ */ new Date()).toISOString()
  };
  const updatedConfigs = [
    ...configs.slice(0, existingIndex),
    updatedConfig,
    ...configs.slice(existingIndex + 1)
  ];
  await service.setValue("ai_assistant", MCP_SERVERS_CONFIG_KEY, updatedConfigs);
  return updatedConfig;
}
async function deleteMcpServerConfig(resolver, serverId) {
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    throw new Error("Configuration service unavailable");
  }
  const configs = await getMcpServerConfigs(resolver);
  const existingIndex = configs.findIndex((c) => c.id === serverId);
  if (existingIndex === -1) {
    return false;
  }
  const updatedConfigs = [
    ...configs.slice(0, existingIndex),
    ...configs.slice(existingIndex + 1)
  ];
  await service.setValue("ai_assistant", MCP_SERVERS_CONFIG_KEY, updatedConfigs);
  return true;
}
async function toggleMcpServerEnabled(resolver, serverId) {
  const config = await getMcpServerConfig(resolver, serverId);
  if (!config) {
    throw new Error(`MCP server config not found: ${serverId}`);
  }
  return updateMcpServerConfig(resolver, serverId, {
    enabled: !config.enabled
  });
}
function generateId() {
  return `mcp_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 9)}`;
}
function validateMcpServerConfig(config) {
  if (!config.name?.trim()) {
    return { valid: false, error: "Name is required" };
  }
  if (!["http", "stdio"].includes(config.type)) {
    return { valid: false, error: 'Type must be "http" or "stdio"' };
  }
  if (config.type === "http") {
    if (!config.url?.trim()) {
      return { valid: false, error: "URL is required for HTTP servers" };
    }
    try {
      new URL(config.url);
    } catch {
      return { valid: false, error: "Invalid URL format" };
    }
  }
  if (config.type === "stdio") {
    if (!config.command?.trim()) {
      return { valid: false, error: "Command is required for stdio servers" };
    }
  }
  return { valid: true };
}
export {
  MCP_SERVERS_CONFIG_KEY,
  deleteMcpServerConfig,
  getEnabledMcpServerConfigs,
  getMcpServerConfig,
  getMcpServerConfigs,
  saveMcpServerConfig,
  toggleMcpServerEnabled,
  updateMcpServerConfig,
  validateMcpServerConfig
};
//# sourceMappingURL=mcp-server-config.js.map
