import { PROVIDER_ENV_MAP, getApiKeyFromEnv, init_env_keys } from "../../../../providers/env-keys.js";
import { getAgentDefaultModelRef, init_schema, parseModelRef } from "../../../../config/schema.js";
import { init_loader, loadConfig } from "../../../../config/loader.js";
import { existsSync } from "node:fs";
//#region src/cli/commands/doctor/checks/provider-auth.ts
init_loader();
init_schema();
init_env_keys();
function collectProviderIdsFromConfig(cfg) {
	const ids = /* @__PURE__ */ new Set();
	const addRef = (ref) => {
		if (!ref?.trim()) return;
		const parsed = parseModelRef(ref.trim());
		if (parsed) ids.add(parsed.provider.toLowerCase());
	};
	addRef(getAgentDefaultModelRef(cfg));
	const raw = cfg.agents?.defaults?.model;
	if (raw && typeof raw === "object" && "fallbacks" in raw && Array.isArray(raw.fallbacks)) for (const f of raw.fallbacks) addRef(typeof f === "string" ? f : void 0);
	const list = cfg.agents?.list;
	if (Array.isArray(list)) for (const e of list) {
		const m = e?.model;
		if (typeof m === "string") addRef(m);
		else if (m && typeof m === "object" && "primary" in m) {
			addRef(typeof m.primary === "string" ? m.primary : void 0);
			const fb = m.fallbacks;
			if (Array.isArray(fb)) for (const f of fb) addRef(f);
		}
	}
	const img = cfg.agents?.defaults?.imageModel;
	if (typeof img === "string") addRef(img);
	else if (img && typeof img === "object" && "primary" in img) addRef(typeof img.primary === "string" ? img.primary : void 0);
	const ss = cfg.agents?.defaults?.sessionSearch?.summaryModel;
	if (typeof ss === "string") addRef(ss);
	return ids;
}
function anyProviderEnvPresent() {
	for (const vars of Object.values(PROVIDER_ENV_MAP)) for (const v of vars) if (process.env[v]?.trim()) return true;
	for (const [k, val] of Object.entries(process.env)) {
		if (!val?.trim()) continue;
		if (k.endsWith("_API_KEY") || k.endsWith("_TOKEN")) return true;
	}
	return false;
}
async function checkProviderAuth(ctx) {
	if (!existsSync(ctx.configPath)) return {
		id: "provider-auth",
		label: "Provider auth",
		status: "skip",
		message: "No config file; skipped.",
		hints: []
	};
	let cfg;
	try {
		cfg = loadConfig(ctx.configPath);
	} catch {
		return {
			id: "provider-auth",
			label: "Provider auth",
			status: "skip",
			message: "Config could not be loaded; skipped.",
			hints: []
		};
	}
	const fromConfig = collectProviderIdsFromConfig(cfg);
	const checkIds = fromConfig.size > 0 ? [...fromConfig] : Object.keys(PROVIDER_ENV_MAP);
	for (const id of checkIds) if (getApiKeyFromEnv(id)?.trim()) return {
		id: "provider-auth",
		label: "Provider auth",
		status: "pass",
		message: "At least one LLM provider API key is available.",
		hints: []
	};
	if (anyProviderEnvPresent()) return {
		id: "provider-auth",
		label: "Provider auth",
		status: "pass",
		message: "Environment contains provider credentials.",
		hints: []
	};
	return {
		id: "provider-auth",
		label: "Provider auth",
		status: "warn",
		message: "No API keys detected for configured providers.",
		hints: ["Set keys via: xopc auth set <provider> <key>", "Or export the provider env vars from the pi-ai docs."]
	};
}
//#endregion
export { checkProviderAuth };

//# sourceMappingURL=provider-auth.js.map