import {
  buildModelDeployment,
  normalizeEmbedding
} from "./chunk-3PATA44Z.js";
import {
  VERSION,
  convertToAISDKError,
  deepMerge,
  getProviderName,
  sapAIEmbeddingProviderOptions
} from "./chunk-6RYKBAYB.js";

// src/foundation-models-embedding-model-strategy.ts
import { TooManyEmbeddingValuesForCallError } from "@ai-sdk/provider";
import { parseProviderOptions } from "@ai-sdk/provider-utils";
var FoundationModelsEmbeddingModelStrategy = class {
  ClientClass;
  /**
   * Creates a new FoundationModelsEmbeddingModelStrategy.
   * @param ClientClass - The AzureOpenAiEmbeddingClient class from `@sap-ai-sdk/foundation-models`.
   */
  constructor(ClientClass) {
    this.ClientClass = ClientClass;
  }
  /**
   * Generates embeddings for the given input values.
   *
   * Validates input count, builds request, calls SAP AI SDK, and normalizes embeddings.
   * @param config - The strategy configuration containing model and deployment info.
   * @param settings - The embedding model settings.
   * @param options - The call options including values to embed and abort signal.
   * @param maxEmbeddingsPerCall - Maximum number of embeddings allowed per call.
   * @returns The embedding result with vectors, usage, and metadata.
   * @throws {TooManyEmbeddingValuesForCallError} If values exceed maxEmbeddingsPerCall.
   * @throws {AISDKError} If the SAP AI SDK call fails.
   */
  async doEmbed(config, settings, options, maxEmbeddingsPerCall) {
    const { abortSignal, providerOptions, values } = options;
    const providerName = getProviderName(config.provider);
    const sapOptions = await parseProviderOptions({
      provider: providerName,
      providerOptions,
      schema: sapAIEmbeddingProviderOptions
    });
    if (values.length > maxEmbeddingsPerCall) {
      throw new TooManyEmbeddingValuesForCallError({
        maxEmbeddingsPerCall,
        modelId: config.modelId,
        provider: config.provider,
        values
      });
    }
    try {
      const client = this.createClient(config, settings.modelVersion);
      const request = this.buildRequest(values, settings, sapOptions);
      const response = await client.run(request, abortSignal ? { signal: abortSignal } : void 0);
      const embeddingData = response.getEmbeddings();
      const tokenUsage = response._data.usage;
      const embeddings = embeddingData.map(
        (embedding) => normalizeEmbedding(embedding)
      );
      const providerMetadata = {
        [providerName]: {
          model: config.modelId,
          version: VERSION
        }
      };
      return {
        embeddings,
        providerMetadata,
        usage: { tokens: tokenUsage.total_tokens },
        warnings: []
      };
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doEmbed",
        requestBody: { values: values.length },
        url: "sap-ai:foundation-models/embeddings"
      });
    }
  }
  /**
   * Builds the embedding request parameters.
   * @param values - The input strings to embed.
   * @param settings - The embedding model settings.
   * @param sapOptions - Provider options from the call.
   * @returns The Azure OpenAI embedding parameters.
   * @internal
   */
  buildRequest(values, settings, sapOptions) {
    const mergedParams = deepMerge(
      settings.modelParams,
      sapOptions?.modelParams
    );
    return {
      input: values,
      ...Object.keys(mergedParams).length > 0 ? mergedParams : {}
    };
  }
  /**
   * Creates an SAP AI SDK AzureOpenAiEmbeddingClient with the given configuration.
   * @param config - The strategy configuration containing deployment info.
   * @param modelVersion - Optional model version for deployment resolution.
   * @returns A new AzureOpenAiEmbeddingClient instance.
   * @internal
   */
  createClient(config, modelVersion) {
    const modelDeployment = buildModelDeployment(config, modelVersion);
    return new this.ClientClass(modelDeployment, config.destination);
  }
};
export {
  FoundationModelsEmbeddingModelStrategy
};
//# sourceMappingURL=foundation-models-embedding-model-strategy-HXF3JQP5.js.map