import { getAgentDefaultModelRef, init_schema, parseModelRef } from "../../config/schema.js";
import { getDefaultModelSync, getModelsByProvider, init_providers, isProviderConfiguredSync } from "../../providers/index.js";
import { resolveAgentModelFallbackValues, resolveAgentModelPrimaryValue } from "../../config/model-input.js";
//#region src/agent/image/tool-model-config.ts
init_schema();
init_providers();
function hasToolModelConfig(model) {
	return Boolean(model?.primary?.trim() || (model?.fallbacks ?? []).some((entry) => entry.trim().length > 0));
}
function resolveDefaultModelRef(cfg) {
	const ref = cfg ? getAgentDefaultModelRef(cfg) : void 0;
	if (ref) {
		const p = parseModelRef(ref);
		if (p) return p;
	}
	const p2 = parseModelRef(getDefaultModelSync(cfg));
	if (p2) return p2;
	return {
		provider: "deepseek",
		model: "deepseek-v4-flash"
	};
}
function coerceToolModelConfig(model) {
	const primary = resolveAgentModelPrimaryValue(model);
	const fallbacks = resolveAgentModelFallbackValues(model);
	return {
		...primary?.trim() ? { primary: primary.trim() } : {},
		...fallbacks.length > 0 ? { fallbacks } : {}
	};
}
function buildToolModelConfigFromCandidates(params) {
	if (hasToolModelConfig(params.explicit)) return params.explicit;
	const deduped = [];
	for (const candidate of params.candidates) {
		const trimmed = candidate?.trim();
		if (!trimmed || !trimmed.includes("/")) continue;
		const provider = trimmed.slice(0, trimmed.indexOf("/")).trim();
		if (!provider || !isProviderConfiguredSync(provider)) continue;
		if (!deduped.includes(trimmed)) deduped.push(trimmed);
	}
	if (deduped.length === 0) return null;
	return {
		primary: deduped[0],
		...deduped.length > 1 ? { fallbacks: deduped.slice(1) } : {}
	};
}
function firstVisionModelRef(provider) {
	const m = getModelsByProvider(provider).find((x) => x.input?.includes("image"));
	return m ? `${provider}/${m.id}` : void 0;
}
/**
* Effective image understanding model: explicit `agents.defaults.imageModel`, else inferred from configured providers.
*/
function resolveImageModelConfigForTool(params) {
	const explicit = coerceToolModelConfig(params.cfg?.agents?.defaults?.imageModel);
	if (hasToolModelConfig(explicit)) return explicit;
	const primary = resolveDefaultModelRef(params.cfg);
	const primaryCandidates = [];
	const vision = firstVisionModelRef(primary.provider);
	if (vision) primaryCandidates.push(vision);
	if (primary.provider === "openai") primaryCandidates.push("openai/gpt-4o-mini");
	if (primary.provider === "anthropic") primaryCandidates.push("anthropic/claude-sonnet-4-5");
	if (primary.provider === "google") primaryCandidates.push("google/gemini-2.0-flash");
	return buildToolModelConfigFromCandidates({
		explicit,
		candidates: [
			...primaryCandidates,
			firstVisionModelRef("openai") ?? "openai/gpt-4o-mini",
			firstVisionModelRef("anthropic") ?? "anthropic/claude-sonnet-4-5"
		]
	});
}
//#endregion
export { buildToolModelConfigFromCandidates, coerceToolModelConfig, hasToolModelConfig, resolveDefaultModelRef, resolveImageModelConfigForTool };

//# sourceMappingURL=tool-model-config.js.map