import { getDefaultModelSync, init_providers, isProviderConfiguredSync } from "../../providers/index.js";
import { normalizeProviderId, parseModelRef as parseModelRef$1 } from "../models/selection.js";
//#region src/agent/fallback/candidates.ts
init_providers();
function getDefaultModelParts(config) {
	const parts = getDefaultModelSync(config).split("/");
	return {
		provider: parts[0] || "anthropic",
		model: parts[1] || "claude-sonnet-4-5"
	};
}
/**
* Parse model reference string into provider/model parts.
* Uses the unified implementation from selection.ts.
*/
function parseModelRef(raw, defaultProvider) {
	const result = parseModelRef$1(raw, defaultProvider);
	if (!result) return null;
	return {
		provider: normalizeProviderId(result.provider),
		model: result.model
	};
}
function resolveFallbackCandidates(params) {
	const { cfg, provider: inputProvider, model: inputModel, fallbacksOverride } = params;
	const modelConfig = cfg?.agents?.defaults?.model;
	const primaryRef = modelConfig?.primary;
	const fallbacks = fallbacksOverride ?? modelConfig?.fallbacks;
	const defaultParts = getDefaultModelParts(cfg);
	const primaryResolved = parseModelRef(primaryRef || getDefaultModelSync(cfg));
	const provider = inputProvider.trim() || primaryResolved?.provider || defaultParts.provider;
	const model = inputModel.trim() || primaryResolved?.model || defaultParts.model;
	const candidates = [];
	const seen = /* @__PURE__ */ new Set();
	const addCandidate = (c) => {
		const key = `${c.provider}/${c.model}`.toLowerCase();
		if (seen.has(key)) return;
		if (!isProviderConfiguredSync(c.provider)) return;
		seen.add(key);
		candidates.push(c);
	};
	addCandidate({
		provider,
		model
	});
	if (fallbacks) for (const fb of fallbacks) {
		const resolved = parseModelRef(fb, provider);
		if (resolved) addCandidate(resolved);
	}
	if (!fallbacksOverride && primaryResolved) addCandidate(primaryResolved);
	return candidates;
}
//#endregion
export { resolveFallbackCandidates };

//# sourceMappingURL=candidates.js.map