import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { OllamaEmbeddingProvider } from "./ollama-embedding-provider.js";
import { OpenAIEmbeddingProvider } from "./openai-embedding-provider.js";
class EmbeddingProviderFactory {
  /**
   * Create an embedding provider based on config.
   * Tries the primary provider first, then fallbacks in order.
   * Returns null if no provider is available.
   */
  static async create(config) {
    const providers = [
      config.provider,
      ...config.fallbackProviders ?? []
    ];
    for (const p of providers) {
      if (p === "none") return null;
      try {
        const provider = await EmbeddingProviderFactory.tryCreate(p, config);
        if (provider) return provider;
      } catch {
      }
    }
    return null;
  }
  static async tryCreate(type, config) {
    switch (type) {
      case "transformers": {
        const { createTransformersProvider } = await import("./transformers-embedding-provider.js");
        return createTransformersProvider(config.model, config.dimension);
      }
      case "ollama":
        return EmbeddingProviderFactory.createOllamaProvider(config);
      case "openai":
        return EmbeddingProviderFactory.createOpenAIProvider(config);
      case "none":
        return null;
      default:
        return null;
    }
  }
  static async createOllamaProvider(config) {
    try {
      const baseUrl = config.baseUrl ?? "http://localhost:11434";
      const response = await fetch(`${baseUrl}/api/tags`, {
        signal: AbortSignal.timeout(2e3)
      });
      if (!response.ok) return null;
      return new OllamaEmbeddingProvider(
        baseUrl,
        config.model ?? "nomic-embed-text",
        config.dimension ?? 768
      );
    } catch {
      return null;
    }
  }
  static async createOpenAIProvider(config) {
    const apiKey = config.apiKey ?? process.env["OPENAI_API_KEY"];
    if (!apiKey) return null;
    return new OpenAIEmbeddingProvider(
      apiKey,
      config.model ?? "text-embedding-3-small",
      config.dimension ?? 1536
    );
  }
}
export {
  EmbeddingProviderFactory
};
//# sourceMappingURL=embedding-provider-factory.js.map
