/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { SettingsService } from '../settings/SettingsService.js';
import { createProviderRuntimeContext, } from '../runtime/providerRuntimeContext.js';
import { createRuntimeInvocationContext, } from '../runtime/RuntimeInvocationContext.js';
import { createRuntimeConfigStub } from './runtime.js';
const DEFAULT_RUNTIME_SOURCE = 'test-utils#createProviderCallOptions';
let runtimeSequence = 0;
function applySettingsOverrides(providerName, settings, overrides) {
    if (!overrides) {
        return;
    }
    if (overrides.global) {
        for (const [key, value] of Object.entries(overrides.global)) {
            settings.set(key, value);
        }
    }
    if (overrides.provider) {
        for (const [key, value] of Object.entries(overrides.provider)) {
            settings.setProviderSetting(providerName, key, value);
        }
    }
}
function buildEphemeralsSnapshot(providerName, settings) {
    const snapshot = {
        ...settings.getAllGlobalSettings(),
    };
    snapshot[providerName] = {
        ...settings.getProviderSettings(providerName),
    };
    return snapshot;
}
function ensureConfig(providerName, settings, explicitConfig, overrides) {
    if (explicitConfig) {
        return explicitConfig;
    }
    const defaultOverrides = {
        getProvider: () => providerName,
        getProviderSettings: () => settings.getProviderSettings(providerName),
        getEphemeralSettings: () => ({
            ...settings.getAllGlobalSettings(),
            ...settings.getProviderSettings(providerName),
        }),
        getEphemeralSetting: (key) => {
            const providerSettings = settings.getProviderSettings(providerName);
            if (key in providerSettings) {
                return providerSettings[key];
            }
            return settings.get(key);
        },
        getSettingsService: () => settings,
    };
    return createRuntimeConfigStub(settings, {
        ...defaultOverrides,
        ...(overrides ?? {}),
    });
}
function ensureRuntime(providerName, settings, config, init) {
    const resolvedRuntimeId = typeof init.runtime?.runtimeId === 'string' &&
        init.runtime.runtimeId.trim().length > 0
        ? init.runtime.runtimeId
        : (init.runtimeId ?? `${providerName}.runtime.${++runtimeSequence}`);
    const runtimeMetadata = {
        source: DEFAULT_RUNTIME_SOURCE,
        ...(init.runtime?.metadata ?? {}),
        ...(init.runtimeMetadata ?? {}),
    };
    if (init.runtime) {
        return {
            ...init.runtime,
            settingsService: settings,
            config: init.runtime.config ?? config,
            runtimeId: resolvedRuntimeId,
            metadata: runtimeMetadata,
        };
    }
    return createProviderRuntimeContext({
        settingsService: settings,
        config,
        runtimeId: resolvedRuntimeId,
        metadata: runtimeMetadata,
    });
}
function ensureInvocation(providerName, settings, runtime, init, metadata) {
    if (init.invocation) {
        return init.invocation;
    }
    const ephemeralsSnapshot = buildEphemeralsSnapshot(providerName, settings);
    const userMemorySnapshot = typeof init.userMemory === 'string' ? init.userMemory : undefined;
    const telemetry = init.resolved && 'telemetry' in init.resolved
        ? init.resolved.telemetry
        : undefined;
    return createRuntimeInvocationContext({
        runtime,
        settings,
        providerName,
        metadata,
        ephemeralsSnapshot,
        userMemory: userMemorySnapshot,
        telemetry,
        fallbackRuntimeId: runtime.runtimeId ?? `${providerName}.runtime`,
    });
}
/**
 * Creates GenerateChatOptions with explicit settings/config/runtime bindings.
 * Tests should prefer this helper over calling provider methods directly
 * with ad-hoc option objects to ensure fail-fast coverage remains intact.
 */
export function createProviderCallOptions(init) {
    if (!init || !init.providerName) {
        throw new Error('createProviderCallOptions requires a providerName to be specified.');
    }
    const settings = init.settings ?? new SettingsService();
    applySettingsOverrides(init.providerName, settings, init.settingsOverrides);
    const config = ensureConfig(init.providerName, settings, init.config, init.configOverrides);
    const runtime = ensureRuntime(init.providerName, settings, config, init);
    const mergedMetadata = {
        ...(runtime.metadata ?? {}),
        ...(init.metadata ?? {}),
    };
    const invocation = ensureInvocation(init.providerName, settings, runtime, init, mergedMetadata);
    return {
        contents: init.contents ?? [],
        tools: init.tools,
        metadata: mergedMetadata,
        settings,
        config,
        runtime,
        invocation,
        resolved: init.resolved,
        userMemory: init.userMemory,
    };
}
//# sourceMappingURL=providerCallOptions.js.map