import { AmplitudeAIError } from "./exceptions.js";
import { tryRequire } from "./utils/resolve-module.js";
import { Anthropic } from "./providers/anthropic.js";
import { Bedrock } from "./providers/bedrock.js";
import { Gemini } from "./providers/gemini.js";
import { Mistral } from "./providers/mistral.js";
import { OpenAI } from "./providers/openai.js";
import { AzureOpenAI } from "./providers/azure-openai.js";
import { GoogleGenAI } from "./providers/google-genai.js";

//#region src/wrappers.ts
/**
* Convenience wrap() function for dependency injection.
*
* When the customer has already created a provider client, wrap()
* returns an instrumented SDK wrapper. OpenAI/Anthropic/Azure clients are
* reconstructed from extracted credentials; Gemini/GoogleGenAI/Bedrock/Mistral
* clients are adopted directly so the caller's configured transport (Vertex AI
* project, AWS region/credentials, custom server URL) is preserved.
*
* Unsupported types raise AmplitudeAIWrapError.
*/
var AmplitudeAIWrapError = class extends AmplitudeAIError {
	constructor(message) {
		super(message);
		this.name = "AmplitudeAIWrapError";
	}
};
function wrap(client, amplitude, opts) {
	const clientObj = client;
	const clientName = (client?.constructor)?.name ?? "unknown";
	const explicitModule = opts?.providerModule;
	const openaiModule = explicitModule && "OpenAI" in explicitModule ? explicitModule : tryRequire("openai");
	if (openaiModule != null) {
		const OpenAIClass = openaiModule.OpenAI;
		const AzureOpenAIClass = openaiModule.AzureOpenAI;
		if (AzureOpenAIClass && client instanceof AzureOpenAIClass) return new AzureOpenAI({
			amplitude,
			apiKey: clientObj.apiKey,
			azureEndpoint: String(clientObj.baseURL ?? ""),
			propagateContext: opts?.propagateContext,
			openaiModule
		});
		if (OpenAIClass && client instanceof OpenAIClass) return new OpenAI({
			amplitude,
			apiKey: clientObj.apiKey,
			baseUrl: clientObj.baseURL != null ? String(clientObj.baseURL) : void 0,
			propagateContext: opts?.propagateContext,
			openaiModule
		});
	}
	const anthropicModule = explicitModule && "Anthropic" in explicitModule ? explicitModule : tryRequire("@anthropic-ai/sdk");
	if (anthropicModule != null) {
		const AnthropicClass = anthropicModule.Anthropic;
		if (AnthropicClass && client instanceof AnthropicClass) return new Anthropic({
			amplitude,
			apiKey: clientObj.apiKey,
			propagateContext: opts?.propagateContext,
			anthropicModule
		});
	}
	const bedrockModule = explicitModule && "BedrockRuntimeClient" in explicitModule ? explicitModule : tryRequire("@aws-sdk/client-bedrock-runtime");
	if (bedrockModule != null) {
		const BedrockRuntimeClient = bedrockModule.BedrockRuntimeClient;
		if (BedrockRuntimeClient && client instanceof BedrockRuntimeClient) return new Bedrock({
			amplitude,
			client,
			bedrockModule
		});
	}
	const googleGenAIModule = explicitModule && "GoogleGenAI" in explicitModule ? explicitModule : tryRequire("@google/genai");
	if (googleGenAIModule != null) {
		const GoogleGenAIClass = googleGenAIModule.GoogleGenAI;
		if (GoogleGenAIClass && client instanceof GoogleGenAIClass) return new GoogleGenAI({
			amplitude,
			client,
			googleGenAIModule
		});
	}
	const geminiModule = explicitModule && "GoogleGenerativeAI" in explicitModule ? explicitModule : tryRequire("@google/generative-ai");
	if (geminiModule != null) {
		const GoogleGenerativeAIClass = geminiModule.GoogleGenerativeAI;
		if (GoogleGenerativeAIClass && client instanceof GoogleGenerativeAIClass) return new Gemini({
			amplitude,
			client,
			geminiModule
		});
	}
	const mistralModule = explicitModule && "Mistral" in explicitModule ? explicitModule : tryRequire("@mistralai/mistralai");
	if (mistralModule != null) {
		const MistralClass = mistralModule.Mistral ?? mistralModule.MistralClient;
		if (MistralClass && client instanceof MistralClass) return new Mistral({
			amplitude,
			client,
			mistralModule
		});
	}
	throw new AmplitudeAIWrapError(`wrap() does not support ${clientName}. Supported types: openai.OpenAI, openai.AzureOpenAI, @anthropic-ai/sdk.Anthropic, @google/genai.GoogleGenAI, @google/generative-ai.GoogleGenerativeAI, @aws-sdk/client-bedrock-runtime.BedrockRuntimeClient, @mistralai/mistralai.Mistral.`);
}

//#endregion
export { AmplitudeAIWrapError, wrap };
//# sourceMappingURL=wrappers.js.map