import { isOpenRouterBaseUrl, resolveConfiguredBaseUrl } from "@steipete/summarize-core";
import { DEFAULT_MINIMAX_BASE_URL, DEFAULT_OLLAMA_BASE_URL } from "../llm/provider-profile.js";
import { resolveCliAvailability, resolveExecutableInPath } from "./environment.js";
export function resolveEnvState({ env, envForRun, configForCli, }) {
    const xaiKeyRaw = typeof envForRun.XAI_API_KEY === "string" ? envForRun.XAI_API_KEY : null;
    const openaiBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.OPENAI_BASE_URL,
        configValue: configForCli?.openai?.baseUrl,
    });
    const nvidiaBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.NVIDIA_BASE_URL,
        configValue: configForCli?.nvidia?.baseUrl,
    });
    const anthropicBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.ANTHROPIC_BASE_URL,
        configValue: configForCli?.anthropic?.baseUrl,
    });
    const googleBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.GOOGLE_BASE_URL ?? envForRun.GEMINI_BASE_URL,
        configValue: configForCli?.google?.baseUrl,
    });
    const xaiBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.XAI_BASE_URL,
        configValue: configForCli?.xai?.baseUrl,
    });
    const zaiBaseUrl = resolveConfiguredBaseUrl({
        envValue: typeof envForRun.Z_AI_BASE_URL === "string"
            ? envForRun.Z_AI_BASE_URL
            : typeof envForRun.ZAI_BASE_URL === "string"
                ? envForRun.ZAI_BASE_URL
                : null,
        configValue: configForCli?.zai?.baseUrl,
    });
    const minimaxBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.MINIMAX_BASE_URL,
        configValue: configForCli?.minimax?.baseUrl,
    });
    const ollamaBaseUrl = resolveConfiguredBaseUrl({
        envValue: envForRun.OLLAMA_BASE_URL,
        configValue: configForCli?.ollama?.baseUrl,
    });
    const zaiKeyRaw = typeof envForRun.Z_AI_API_KEY === "string"
        ? envForRun.Z_AI_API_KEY
        : typeof envForRun.ZAI_API_KEY === "string"
            ? envForRun.ZAI_API_KEY
            : null;
    const openRouterKeyRaw = typeof envForRun.OPENROUTER_API_KEY === "string" ? envForRun.OPENROUTER_API_KEY : null;
    const openaiKeyRaw = typeof envForRun.OPENAI_API_KEY === "string" ? envForRun.OPENAI_API_KEY : null;
    const nvidiaKeyRaw = typeof envForRun.NVIDIA_API_KEY === "string"
        ? envForRun.NVIDIA_API_KEY
        : typeof envForRun.NGC_API_KEY === "string"
            ? envForRun.NGC_API_KEY
            : null;
    const minimaxKeyRaw = typeof envForRun.MINIMAX_API_KEY === "string" ? envForRun.MINIMAX_API_KEY : null;
    const apiKey = typeof openaiBaseUrl === "string" && isOpenRouterBaseUrl(openaiBaseUrl)
        ? (openRouterKeyRaw ?? openaiKeyRaw)
        : openaiKeyRaw;
    const apifyToken = typeof envForRun.APIFY_API_TOKEN === "string" ? envForRun.APIFY_API_TOKEN : null;
    const ytDlpPath = (() => {
        const explicit = typeof envForRun.YT_DLP_PATH === "string" ? envForRun.YT_DLP_PATH.trim() : "";
        if (explicit.length > 0)
            return explicit;
        return resolveExecutableInPath("yt-dlp", envForRun);
    })();
    const ytDlpCookiesFromBrowser = (() => {
        const raw = typeof envForRun.SUMMARIZE_YT_DLP_COOKIES_FROM_BROWSER === "string"
            ? envForRun.SUMMARIZE_YT_DLP_COOKIES_FROM_BROWSER
            : typeof envForRun.YT_DLP_COOKIES_FROM_BROWSER === "string"
                ? envForRun.YT_DLP_COOKIES_FROM_BROWSER
                : "";
        const value = raw.trim();
        return value.length > 0 ? value : null;
    })();
    const groqApiKey = typeof envForRun.GROQ_API_KEY === "string" ? envForRun.GROQ_API_KEY.trim() || null : null;
    const assemblyaiApiKey = typeof envForRun.ASSEMBLYAI_API_KEY === "string"
        ? envForRun.ASSEMBLYAI_API_KEY.trim() || null
        : null;
    const elevenlabsApiKey = typeof envForRun.ELEVENLABS_API_KEY === "string"
        ? envForRun.ELEVENLABS_API_KEY.trim() || null
        : null;
    const falApiKey = typeof envForRun.FAL_KEY === "string" ? envForRun.FAL_KEY : null;
    const firecrawlKey = typeof envForRun.FIRECRAWL_API_KEY === "string" ? envForRun.FIRECRAWL_API_KEY : null;
    const anthropicKeyRaw = typeof envForRun.ANTHROPIC_API_KEY === "string" ? envForRun.ANTHROPIC_API_KEY : null;
    const googleKeyRaw = typeof envForRun.GEMINI_API_KEY === "string"
        ? envForRun.GEMINI_API_KEY
        : typeof envForRun.GOOGLE_GENERATIVE_AI_API_KEY === "string"
            ? envForRun.GOOGLE_GENERATIVE_AI_API_KEY
            : typeof envForRun.GOOGLE_API_KEY === "string"
                ? envForRun.GOOGLE_API_KEY
                : null;
    const firecrawlApiKey = firecrawlKey && firecrawlKey.trim().length > 0 ? firecrawlKey : null;
    const firecrawlConfigured = firecrawlApiKey !== null;
    const xaiApiKey = xaiKeyRaw?.trim() ?? null;
    const zaiApiKey = zaiKeyRaw?.trim() ?? null;
    const zaiBaseUrlEffective = (zaiBaseUrl?.trim() ?? "") || "https://api.z.ai/api/paas/v4";
    const nvidiaApiKey = nvidiaKeyRaw?.trim() ?? null;
    const nvidiaBaseUrlEffective = (nvidiaBaseUrl?.trim() ?? "") || "https://integrate.api.nvidia.com/v1";
    const minimaxApiKey = minimaxKeyRaw?.trim() ?? null;
    const minimaxBaseUrlEffective = (minimaxBaseUrl?.trim() ?? "") || DEFAULT_MINIMAX_BASE_URL;
    const ollamaBaseUrlEffective = (ollamaBaseUrl?.trim() ?? "") || DEFAULT_OLLAMA_BASE_URL;
    const googleApiKey = googleKeyRaw?.trim() ?? null;
    const anthropicApiKey = anthropicKeyRaw?.trim() ?? null;
    const openrouterApiKey = (() => {
        const explicit = openRouterKeyRaw?.trim() ?? "";
        if (explicit.length > 0)
            return explicit;
        const baseUrl = openaiBaseUrl ?? "";
        const openaiKey = openaiKeyRaw?.trim() ?? "";
        if (baseUrl.length > 0 && isOpenRouterBaseUrl(baseUrl) && openaiKey.length > 0) {
            return openaiKey;
        }
        return null;
    })();
    const openaiApiKey = openaiKeyRaw?.trim() ?? null;
    const googleConfigured = typeof googleApiKey === "string" && googleApiKey.length > 0;
    const anthropicConfigured = typeof anthropicApiKey === "string" && anthropicApiKey.length > 0;
    const openrouterConfigured = typeof openrouterApiKey === "string" && openrouterApiKey.length > 0;
    const cliAvailability = resolveCliAvailability({ env, config: configForCli });
    const envForAuto = openrouterApiKey
        ? { ...envForRun, OPENROUTER_API_KEY: openrouterApiKey }
        : envForRun;
    const providerBaseUrls = {
        openai: openaiBaseUrl,
        nvidia: nvidiaBaseUrl,
        anthropic: anthropicBaseUrl,
        google: googleBaseUrl,
        xai: xaiBaseUrl,
    };
    return {
        apiKey: apiKey?.trim() ?? null,
        openrouterApiKey,
        openrouterConfigured,
        groqApiKey,
        assemblyaiApiKey,
        elevenlabsApiKey,
        openaiApiKey,
        xaiApiKey,
        googleApiKey,
        anthropicApiKey,
        zaiApiKey,
        zaiBaseUrl: zaiBaseUrlEffective,
        nvidiaApiKey,
        nvidiaBaseUrl: nvidiaBaseUrlEffective,
        minimaxApiKey,
        minimaxBaseUrl: minimaxBaseUrlEffective,
        ollamaBaseUrl: ollamaBaseUrlEffective,
        firecrawlApiKey,
        firecrawlConfigured,
        googleConfigured,
        anthropicConfigured,
        apifyToken,
        ytDlpPath,
        ytDlpCookiesFromBrowser,
        falApiKey,
        cliAvailability,
        envForAuto,
        providerBaseUrls,
    };
}
//# sourceMappingURL=environment-state.js.map