import { Ec as listProviderBrandManifests, Is as PROVIDER_BRAND_ORDER } from "./agent-scope-CLn_Ws3B.js";
import { It as resolveManifestProviderAuthChoices, Lt as resolveManifestProviderOnboardAuthFlags } from "./auth-choice-DbEAsUsi.js";

//#region src/commands/auth-choice-legacy.ts
const AUTH_CHOICE_LEGACY_ALIASES_FOR_CLI = [
	"setup-token",
	"oauth",
	"claude-cli",
	"codex-cli",
	"minimax-cloud",
	"minimax"
];
function normalizeLegacyOnboardAuthChoice(authChoice) {
	if (authChoice === "oauth") return "anthropic-oauth";
	if (authChoice === "claude-cli") return "setup-token";
	if (authChoice === "codex-cli") return "openai-codex";
	return authChoice;
}
function isDeprecatedAuthChoice(authChoice) {
	return authChoice === "claude-cli" || authChoice === "codex-cli";
}

//#endregion
//#region src/commands/onboard-provider-auth-flags.ts
const ONBOARD_PROVIDER_AUTH_FLAGS = [
	{
		optionKey: "anthropicApiKey",
		authChoice: "apiKey",
		cliFlag: "--anthropic-api-key",
		cliOption: "--anthropic-api-key <key>",
		description: "Anthropic API key"
	},
	{
		optionKey: "openaiApiKey",
		authChoice: "openai-api-key",
		cliFlag: "--openai-api-key",
		cliOption: "--openai-api-key <key>",
		description: "OpenAI API key"
	},
	{
		optionKey: "mistralApiKey",
		authChoice: "mistral-api-key",
		cliFlag: "--mistral-api-key",
		cliOption: "--mistral-api-key <key>",
		description: "Mistral API key"
	},
	{
		optionKey: "openrouterApiKey",
		authChoice: "openrouter-api-key",
		cliFlag: "--openrouter-api-key",
		cliOption: "--openrouter-api-key <key>",
		description: "OpenRouter API key"
	},
	{
		optionKey: "aiGatewayApiKey",
		authChoice: "ai-gateway-api-key",
		cliFlag: "--ai-gateway-api-key",
		cliOption: "--ai-gateway-api-key <key>",
		description: "Vercel AI API key"
	},
	{
		optionKey: "cloudflareAiGatewayApiKey",
		authChoice: "cloudflare-ai-gateway-api-key",
		cliFlag: "--cloudflare-ai-gateway-api-key",
		cliOption: "--cloudflare-ai-gateway-api-key <key>",
		description: "Cloudflare AI API key"
	},
	{
		optionKey: "moonshotApiKey",
		authChoice: "moonshot-api-key",
		cliFlag: "--moonshot-api-key",
		cliOption: "--moonshot-api-key <key>",
		description: "Moonshot API key"
	},
	{
		optionKey: "kimiCodeApiKey",
		authChoice: "kimi-code-api-key",
		cliFlag: "--kimi-code-api-key",
		cliOption: "--kimi-code-api-key <key>",
		description: "Kimi Coding API key"
	},
	{
		optionKey: "geminiApiKey",
		authChoice: "gemini-api-key",
		cliFlag: "--gemini-api-key",
		cliOption: "--gemini-api-key <key>",
		description: "Gemini API key"
	},
	{
		optionKey: "zaiApiKey",
		authChoice: "zai-api-key",
		cliFlag: "--zai-api-key",
		cliOption: "--zai-api-key <key>",
		description: "Z.AI API key"
	},
	{
		optionKey: "xiaomiApiKey",
		authChoice: "xiaomi-api-key",
		cliFlag: "--xiaomi-api-key",
		cliOption: "--xiaomi-api-key <key>",
		description: "Xiaomi API key"
	},
	{
		optionKey: "minimaxApiKey",
		authChoice: "minimax-api",
		cliFlag: "--minimax-api-key",
		cliOption: "--minimax-api-key <key>",
		description: "MiniMax API key"
	},
	{
		optionKey: "syntheticApiKey",
		authChoice: "synthetic-api-key",
		cliFlag: "--synthetic-api-key",
		cliOption: "--synthetic-api-key <key>",
		description: "Synthetic API key"
	},
	{
		optionKey: "veniceApiKey",
		authChoice: "venice-api-key",
		cliFlag: "--venice-api-key",
		cliOption: "--venice-api-key <key>",
		description: "Venice API key"
	},
	{
		optionKey: "togetherApiKey",
		authChoice: "together-api-key",
		cliFlag: "--together-api-key",
		cliOption: "--together-api-key <key>",
		description: "Together AI API key"
	},
	{
		optionKey: "huggingfaceApiKey",
		authChoice: "huggingface-api-key",
		cliFlag: "--huggingface-api-key",
		cliOption: "--huggingface-api-key <key>",
		description: "Hugging Face API key (HF token)"
	},
	{
		optionKey: "opencodeZenApiKey",
		authChoice: "opencode-zen",
		cliFlag: "--opencode-zen-api-key",
		cliOption: "--opencode-zen-api-key <key>",
		description: "OpenCode Zen API key"
	},
	{
		optionKey: "xaiApiKey",
		authChoice: "xai-api-key",
		cliFlag: "--xai-api-key",
		cliOption: "--xai-api-key <key>",
		description: "xAI API key"
	},
	{
		optionKey: "litellmApiKey",
		authChoice: "litellm-api-key",
		cliFlag: "--litellm-api-key",
		cliOption: "--litellm-api-key <key>",
		description: "LiteLLM API key"
	},
	{
		optionKey: "qianfanApiKey",
		authChoice: "qianfan-api-key",
		cliFlag: "--qianfan-api-key",
		cliOption: "--qianfan-api-key <key>",
		description: "QIANFAN API key"
	},
	{
		optionKey: "qwenCodingPlanApiKey",
		authChoice: "qwen-coding-plan-api-key",
		cliFlag: "--qwen-coding-plan-api-key",
		cliOption: "--qwen-coding-plan-api-key <key>",
		description: "Qwen Coding Plan API key"
	},
	{
		optionKey: "qwenApiKey",
		authChoice: "qwen-api-key",
		cliFlag: "--qwen-api-key",
		cliOption: "--qwen-api-key <key>",
		description: "Qwen DashScope API key"
	},
	{
		optionKey: "volcengineApiKey",
		authChoice: "volcengine-api-key",
		cliFlag: "--volcengine-api-key",
		cliOption: "--volcengine-api-key <key>",
		description: "Volcano Engine API key"
	},
	{
		optionKey: "byteplusApiKey",
		authChoice: "byteplus-api-key",
		cliFlag: "--byteplus-api-key",
		cliOption: "--byteplus-api-key <key>",
		description: "BytePlus API key"
	}
];
function mapManifestFlag(flag) {
	return {
		optionKey: flag.optionKey,
		authChoice: flag.authChoice,
		cliFlag: flag.cliFlag,
		cliOption: flag.cliOption,
		description: flag.description
	};
}
function resolveOnboardProviderAuthFlags(params) {
	const merged = [...ONBOARD_PROVIDER_AUTH_FLAGS];
	const seen = new Set(merged.map((flag) => `${String(flag.optionKey)}::${flag.cliFlag}`));
	for (const manifestFlag of resolveManifestProviderOnboardAuthFlags({
		config: params?.config,
		workspaceDir: params?.workspaceDir,
		env: params?.env,
		includeUntrustedWorkspacePlugins: false
	})) {
		const mapped = mapManifestFlag(manifestFlag);
		const dedupeKey = `${String(mapped.optionKey)}::${mapped.cliFlag}`;
		if (seen.has(dedupeKey)) continue;
		seen.add(dedupeKey);
		merged.push(mapped);
	}
	return merged;
}

//#endregion
//#region src/commands/auth-choice-options.ts
function providerManifestToAuthChoiceGroupDef(manifest) {
	return {
		value: manifest.id,
		label: manifest.label,
		hint: manifest.hint,
		choices: manifest.methods.map((method) => method.id)
	};
}
const MANIFEST_AUTH_CHOICE_GROUP_DEFS = listProviderBrandManifests().map(providerManifestToAuthChoiceGroupDef);
const STATIC_AUTH_CHOICE_GROUP_DEFS = [];
const AUTH_CHOICE_GROUP_DEF_BY_ID = new Map([...MANIFEST_AUTH_CHOICE_GROUP_DEFS, ...STATIC_AUTH_CHOICE_GROUP_DEFS].map((group) => [group.value, group]));
const AUTH_CHOICE_GROUP_DEFS = [...PROVIDER_BRAND_ORDER.flatMap((providerId) => {
	const group = AUTH_CHOICE_GROUP_DEF_BY_ID.get(providerId);
	if (!group) return [];
	AUTH_CHOICE_GROUP_DEF_BY_ID.delete(providerId);
	return [group];
}), ...AUTH_CHOICE_GROUP_DEF_BY_ID.values()];
const PROVIDER_AUTH_CHOICE_OPTION_HINTS = {};
const PROVIDER_AUTH_CHOICE_OPTION_LABELS = {
	"moonshot-api-key": "Kimi API key (.ai)",
	"moonshot-api-key-cn": "Kimi API key (.cn)",
	"kimi-code-api-key": "Kimi Code API key (subscription)"
};
function isVisibleManifestAuthChoice(choice) {
	return choice.assistantVisibility !== "manual-only";
}
function titleCaseProviderLabel(providerId) {
	return providerId.split(/[-_]/g).filter((part) => part.length > 0).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join(" ");
}
function mergeUniqueAuthChoiceOptions(...lists) {
	const merged = [];
	const seen = /* @__PURE__ */ new Set();
	for (const list of lists) for (const option of list) {
		const key = String(option.value);
		if (seen.has(key)) continue;
		seen.add(key);
		merged.push(option);
	}
	return merged;
}
function buildProviderAuthChoiceOptions() {
	return ONBOARD_PROVIDER_AUTH_FLAGS.map((flag) => ({
		value: flag.authChoice,
		label: PROVIDER_AUTH_CHOICE_OPTION_LABELS[flag.authChoice] ?? flag.description,
		...PROVIDER_AUTH_CHOICE_OPTION_HINTS[flag.authChoice] ? { hint: PROVIDER_AUTH_CHOICE_OPTION_HINTS[flag.authChoice] } : {}
	}));
}
function buildManifestAuthChoiceOptions() {
	return mergeUniqueAuthChoiceOptions(resolveManifestProviderAuthChoices({ includeUntrustedWorkspacePlugins: false }).filter(isVisibleManifestAuthChoice).map((choice) => ({
		value: choice.choiceId,
		label: choice.choiceLabel,
		...choice.choiceHint ? { hint: choice.choiceHint } : {}
	})));
}
function buildRegistryProviderAuthChoiceOptions() {
	return listProviderBrandManifests().flatMap((manifest) => manifest.methods).map((method) => ({
		value: method.id,
		label: method.label,
		hint: method.hint
	}));
}
function buildManifestAuthChoiceGroupDefs() {
	const groups = /* @__PURE__ */ new Map();
	for (const choice of resolveManifestProviderAuthChoices({ includeUntrustedWorkspacePlugins: false }).filter(isVisibleManifestAuthChoice)) {
		const groupId = choice.groupId ?? choice.providerId;
		const existing = groups.get(String(groupId));
		if (existing) {
			if (!existing.choices.includes(choice.choiceId)) existing.choices.push(choice.choiceId);
			if (!existing.hint && choice.groupHint) existing.hint = choice.groupHint;
			continue;
		}
		groups.set(String(groupId), {
			value: groupId,
			label: choice.groupLabel ?? titleCaseProviderLabel(choice.providerId),
			...choice.groupHint ? { hint: choice.groupHint } : {},
			choices: [choice.choiceId]
		});
	}
	return [...groups.values()];
}
function resolveAuthChoiceGroupDefs() {
	const merged = AUTH_CHOICE_GROUP_DEFS.map((group) => ({
		...group,
		choices: [...group.choices]
	}));
	const groupByValue = new Map(merged.map((group) => [String(group.value), group]));
	for (const manifestGroup of buildManifestAuthChoiceGroupDefs()) {
		const existing = groupByValue.get(String(manifestGroup.value));
		if (!existing) {
			const appended = {
				...manifestGroup,
				choices: [...manifestGroup.choices]
			};
			merged.push(appended);
			groupByValue.set(String(manifestGroup.value), appended);
			continue;
		}
		for (const choice of manifestGroup.choices) if (!existing.choices.includes(choice)) existing.choices.push(choice);
		if (!existing.hint && manifestGroup.hint) existing.hint = manifestGroup.hint;
	}
	return merged;
}
function resolveBaseAuthChoiceOptions() {
	return mergeUniqueAuthChoiceOptions(buildRegistryProviderAuthChoiceOptions(), STATIC_BASE_AUTH_CHOICE_OPTIONS, buildManifestAuthChoiceOptions());
}
const STATIC_BASE_AUTH_CHOICE_OPTIONS = [
	...buildProviderAuthChoiceOptions(),
	{
		value: "moonshot-api-key-cn",
		label: "Kimi API key (.cn)"
	},
	{
		value: "zai-api-key",
		label: "Z.AI API key"
	},
	{
		value: "xiaomi-api-key",
		label: "Xiaomi API key"
	}
];
function formatAuthChoiceChoicesForCli(params) {
	const includeSkip = params?.includeSkip ?? true;
	const includeLegacyAliases = params?.includeLegacyAliases ?? false;
	const values = resolveBaseAuthChoiceOptions().map((opt) => opt.value);
	if (includeSkip) values.push("skip");
	if (includeLegacyAliases) values.push(...AUTH_CHOICE_LEGACY_ALIASES_FOR_CLI);
	return values.join("|");
}
function buildAuthChoiceOptions(params) {
	params.store;
	const options = resolveBaseAuthChoiceOptions();
	if (params.includeSkip) options.push({
		value: "skip",
		label: "Skip for now"
	});
	return options;
}
function buildAuthChoiceGroups(params) {
	const options = buildAuthChoiceOptions({
		...params,
		includeSkip: false
	});
	const optionByValue = new Map(options.map((opt) => [opt.value, opt]));
	return {
		groups: resolveAuthChoiceGroupDefs().map((group) => ({
			...group,
			options: group.choices.map((choice) => optionByValue.get(choice)).filter((opt) => Boolean(opt))
		})),
		skipOption: params.includeSkip ? {
			value: "skip",
			label: "Skip for now"
		} : void 0
	};
}

//#endregion
//#region src/commands/auth-choice-prompt.ts
const BACK_VALUE = "__back";
async function promptAuthChoiceGrouped(params) {
	const { groups, skipOption } = buildAuthChoiceGroups(params);
	const availableGroups = groups.filter((group) => group.options.length > 0);
	while (true) {
		const providerOptions = [...availableGroups.map((group) => ({
			value: group.value,
			label: group.label,
			hint: group.hint
		})), ...skipOption ? [skipOption] : []];
		const providerSelection = await params.prompter.select({
			message: "Model/auth provider",
			options: providerOptions
		});
		if (providerSelection === "skip") return "skip";
		const group = availableGroups.find((candidate) => candidate.value === providerSelection);
		if (!group || group.options.length === 0) {
			await params.prompter.note("No auth methods available for that provider.", "Model/auth choice");
			continue;
		}
		if (group.options.length === 1) return group.options[0].value;
		const methodSelection = await params.prompter.select({
			message: `${group.label} auth method`,
			options: [...group.options, {
				value: BACK_VALUE,
				label: "Back"
			}]
		});
		if (methodSelection === BACK_VALUE) continue;
		return methodSelection;
	}
}

//#endregion
export { normalizeLegacyOnboardAuthChoice as a, isDeprecatedAuthChoice as i, formatAuthChoiceChoicesForCli as n, resolveOnboardProviderAuthFlags as r, promptAuthChoiceGrouped as t };