import { n as ensureAuthProfileStore } from "./store-DLavseDR.js";
import "./auth-profiles-y8pTQOqY.js";
import { p as resolveDefaultModelForAgent } from "./model-selection-DCVjDyLz.js";
import { n as listProfilesForProvider } from "./profiles-g4Nk53sC.js";
import { t as resolveEnvApiKey } from "./model-auth-env-Bh0gQQ5c.js";
import { i as loadModelCatalog } from "./model-catalog-B5rUL5wO.js";
import { o as hasUsableCustomProviderApiKey } from "./model-auth-BqWY8X0_.js";
import { t as buildProviderAuthRecoveryHint } from "./provider-auth-guidance-u4qtbVMM.js";
import { r as normalizeLegacyOnboardAuthChoice } from "./auth-choice-legacy-waB0iFpm.js";
import { t as applyAuthChoiceLoadedPluginProvider } from "./provider-auth-choice-BsQSSh4a.js";
import { n as applyAuthChoiceApiProviders, r as normalizeApiKeyTokenProviderAuthChoice } from "./provider-auth-choice-preference-BSIASRiq.js";
//#region src/commands/auth-choice.apply.oauth.ts
async function applyAuthChoiceOAuth(_params) {
	return null;
}
//#endregion
//#region src/commands/auth-choice.apply.ts
async function applyAuthChoice(params) {
	const normalizedProviderAuthChoice = normalizeApiKeyTokenProviderAuthChoice({
		authChoice: normalizeLegacyOnboardAuthChoice(params.authChoice, {
			config: params.config,
			env: params.env
		}) ?? params.authChoice,
		tokenProvider: params.opts?.tokenProvider,
		config: params.config,
		env: params.env
	});
	const normalizedParams = normalizedProviderAuthChoice === params.authChoice ? params : {
		...params,
		authChoice: normalizedProviderAuthChoice
	};
	const handlers = [
		applyAuthChoiceLoadedPluginProvider,
		applyAuthChoiceOAuth,
		applyAuthChoiceApiProviders
	];
	for (const handler of handlers) {
		const result = await handler(normalizedParams);
		if (result) return result;
	}
	if (normalizedParams.authChoice === "token" || normalizedParams.authChoice === "setup-token") throw new Error([`Auth choice "${normalizedParams.authChoice}" was not matched to a provider setup flow.`, "For Anthropic legacy token auth, use \"setup-token\" with tokenProvider=\"anthropic\" or choose the Anthropic setup-token entry explicitly."].join("\n"));
	if (normalizedParams.authChoice === "oauth") throw new Error("Auth choice \"oauth\" is no longer supported directly. Use \"setup-token\" for Anthropic legacy token auth or a provider-specific OAuth entry.");
	return { config: normalizedParams.config };
}
//#endregion
//#region src/commands/auth-choice.model-check.ts
async function warnIfModelConfigLooksOff(config, prompter, options) {
	const ref = resolveDefaultModelForAgent({
		cfg: config,
		agentId: options?.agentId
	});
	const warnings = [];
	const catalog = await loadModelCatalog({
		config,
		useCache: false
	});
	if (catalog.length > 0) {
		if (!catalog.some((entry) => entry.provider === ref.provider && entry.id === ref.model)) warnings.push(`Model not found: ${ref.provider}/${ref.model}. Update agents.defaults.model or run /models list.`);
	}
	const hasProfile = listProfilesForProvider(ensureAuthProfileStore(options?.agentDir), ref.provider).length > 0;
	const envKey = resolveEnvApiKey(ref.provider);
	const hasCustomKey = hasUsableCustomProviderApiKey(config, ref.provider);
	if (!hasProfile && !envKey && !hasCustomKey) warnings.push(`No auth configured for provider "${ref.provider}". The agent may fail until credentials are added. ${buildProviderAuthRecoveryHint({
		provider: ref.provider,
		config,
		includeEnvVar: true
	})}`);
	if (warnings.length > 0) await prompter.note(warnings.join("\n"), "Model check");
}
//#endregion
export { applyAuthChoice as n, warnIfModelConfigLooksOff as t };
