import { CLOUD_ENDPOINTS } from '../chunks/constants.mjs';
import { C as CloudApiClient } from './ccjk.D6ycHbak.mjs';

const ROUTE_MAP = {
  // Analysis endpoints
  "analysis.projects": {
    v1: "/api/v1/analysis/projects",
    v8: "/api/v8/analyze"
    // Legacy v8 path
  },
  // Template endpoints
  "templates.get": {
    v1: "/api/v1/templates/:id"
  },
  "templates.batch": {
    v1: "/api/v1/templates/batch",
    v8: "/api/v8/templates"
    // Legacy v8 batch endpoint
  },
  // Telemetry endpoints
  "telemetry.installation": {
    v1: "/api/v1/telemetry/installation"
  },
  // Health check
  "health": {
    v1: "/api/v1/health"
  },
  // Plugin endpoints
  "plugins.list": {
    v1: "/v1/plugins/list"
  },
  "plugins.recommend": {
    v1: "/v1/plugins/recommend"
  },
  // Skills endpoints
  "skills.list": {
    v1: "/v1/skills/list"
  },
  "skills.upload": {
    v1: "/v1/skills/upload"
  },
  "skills.download": {
    v1: "/v1/skills/download"
  },
  // Notification endpoints (remote API)
  "notifications.bind": {
    v1: "/api/v1/bind/use"
  },
  "notifications.send": {
    v1: "/api/v1/notify"
  },
  "notifications.poll": {
    v1: "/api/v1/reply/poll"
  }
};
class CloudApiGateway {
  mainClient;
  pluginsClient;
  remoteClient;
  config;
  constructor(config = {}) {
    this.config = {
      timeout: config.timeout || 3e4,
      authToken: config.authToken || "",
      enableVersionFallback: config.enableVersionFallback ?? true
    };
    this.mainClient = new CloudApiClient({
      baseUrl: CLOUD_ENDPOINTS.MAIN.BASE_URL,
      timeout: this.config.timeout,
      authToken: this.config.authToken
    });
    this.pluginsClient = new CloudApiClient({
      baseUrl: CLOUD_ENDPOINTS.PLUGINS.BASE_URL,
      timeout: this.config.timeout,
      authToken: this.config.authToken
    });
    this.remoteClient = new CloudApiClient({
      baseUrl: CLOUD_ENDPOINTS.REMOTE.BASE_URL,
      timeout: this.config.timeout,
      authToken: this.config.authToken
    });
  }
  /**
   * Make a request through the gateway
   *
   * @param route - Semantic route identifier
   * @param options - Request options
   * @returns API response
   */
  async request(route, options) {
    const routeConfig = ROUTE_MAP[route];
    if (!routeConfig) {
      return {
        success: false,
        error: `Unknown route: ${route}`,
        code: "INVALID_ROUTE"
      };
    }
    const client = this.getClientForRoute(route);
    const version = options.preferVersion || "v1";
    const path = this.resolvePath(routeConfig, version);
    const response = await client.request(path, {
      method: options.method,
      body: options.body,
      query: options.query,
      authToken: options.authToken || this.config.authToken,
      timeout: options.timeout || this.config.timeout
    });
    if (!response.success && version === "v1" && routeConfig.v8 && this.config.enableVersionFallback) {
      const v8Path = routeConfig.v8;
      return client.request(v8Path, {
        method: options.method,
        body: options.body,
        query: options.query,
        authToken: options.authToken || this.config.authToken,
        timeout: options.timeout || this.config.timeout
      });
    }
    return response;
  }
  /**
   * Set authentication token for all clients
   */
  setAuthToken(token) {
    this.config.authToken = token;
    this.mainClient.setAuthToken(token);
    this.pluginsClient.setAuthToken(token);
    this.remoteClient.setAuthToken(token);
  }
  /**
   * Get current configuration
   */
  getConfig() {
    return { ...this.config };
  }
  // ==========================================================================
  // Private Methods
  // ==========================================================================
  /**
   * Get appropriate client for a route
   */
  getClientForRoute(route) {
    if (route.startsWith("plugins.")) {
      return this.pluginsClient;
    }
    if (route.startsWith("notifications.")) {
      return this.remoteClient;
    }
    if (route.startsWith("skills.")) {
      return this.pluginsClient;
    }
    return this.mainClient;
  }
  /**
   * Resolve path for specific version
   */
  resolvePath(routeConfig, version) {
    return version === "v8" && routeConfig.v8 ? routeConfig.v8 : routeConfig.v1;
  }
}
function createGateway(config) {
  return new CloudApiGateway(config);
}
function createDefaultGateway() {
  return new CloudApiGateway({
    timeout: 3e4,
    enableVersionFallback: true
  });
}

export { createGateway as a, createDefaultGateway as c };
