/**
 * LLM client factory — hoisted from packages/eval/src/composition-root.ts
 * so packages/api can build a DiagnosisRunner without importing eval (D-01).
 *
 * Adapter CLASSES stay in packages/eval/src/adapters/llm/. Only the factory
 * function lives here. Adapter constructors are injected via `LLMClientAdapters`
 * so core never static-imports vendor SDK code (D0051 invariant / T-05-01-01).
 *
 * @see docs/decisions/D0051-llm-client-port.md
 * @see packages/eval/src/composition-root.ts — call site (updated to use this)
 */
// ---------------------------------------------------------------------------
// Factory function
// ---------------------------------------------------------------------------
/**
 * Select the LLMClient adapter based on `config.llmProvider` and the
 * supplied API keys. Returns `undefined` when no usable credential is
 * present — `AppContext.llmClient` stays unset and consumers handle that
 * explicitly.
 *
 * Adapters never read `process.env` themselves (per
 * `.claude/rules/typescript.md`); env mapping happens at the call site
 * (typically `createAppContext`).
 *
 * Adapter classes stay in packages/eval; they are passed in via `deps.adapters`
 * so this factory has zero eval imports (D-01 / T-05-01-01).
 */
export function createLLMClient(config, keys, deps) {
    const { logger, adapters } = deps;
    const explicit = config.llmProvider;
    const anthropicKey = keys.anthropicApiKey;
    const openaiKey = keys.openaiApiKey;
    // Auto-select: prefer Anthropic when both are present (matches the
    // current grader's default model in config/models.ts).
    const provider = explicit ?? (anthropicKey ? "anthropic" : openaiKey ? "openai" : undefined);
    if (!provider) {
        logger.debug("LLM client: not wired — no Anthropic or OpenAI API key supplied");
        return undefined;
    }
    if (provider === "anthropic") {
        if (!anthropicKey) {
            logger.warn('llmProvider="anthropic" but no Anthropic API key supplied — LLMClient not wired');
            return undefined;
        }
        logger.debug("LLM client: AnthropicLLMClient");
        return adapters.newAnthropicClient({ apiKey: anthropicKey, logger });
    }
    if (!openaiKey) {
        logger.warn('llmProvider="openai" but no OpenAI API key supplied — LLMClient not wired');
        return undefined;
    }
    logger.debug("LLM client: OpenAILLMClient");
    return adapters.newOpenAIClient({ apiKey: openaiKey, logger });
}
