import { init_agent_scope, listAgentEntries } from "../agent/agent-scope.js";
import { init_schema, parseModelRef } from "./schema.js";
//#region src/config/agent-typed-models.ts
init_agent_scope();
init_schema();
/**
* Merge defaults + per-agent typed models. Agent entry wins on same `id`.
*/
function mergeTypedModels(defaults, entry) {
	const out = /* @__PURE__ */ new Map();
	for (const m of defaults ?? []) out.set(m.id, m);
	for (const m of entry ?? []) out.set(m.id, m);
	return out;
}
function resolveEffectiveTypedModels(config, agentId) {
	const defaults = config.agents?.defaults?.models;
	const entry = listAgentEntries(config).find((a) => a.enabled !== false && a.id.toLowerCase() === agentId.toLowerCase())?.models;
	return mergeTypedModels(defaults, entry);
}
/** Resolve a typed model id to `provider/model`, or undefined when not configured. */
function resolveTypedModelRef(config, agentId, typeId) {
	return resolveEffectiveTypedModels(config, agentId).get(typeId)?.model?.trim() || void 0;
}
function parseModelRefInput(ref) {
	const trimmed = ref.trim();
	if (!trimmed) return {};
	if (trimmed.includes("/")) return { directRef: trimmed };
	const typeId = trimmed.startsWith("@") ? trimmed.slice(1).trim() : trimmed;
	return typeId ? { typeId } : {};
}
/**
* Resolve a model reference: `provider/model`, typed id (`small`), or `@small`.
* Throws when the ref is invalid or the typed id is unknown.
*/
function resolveModelRef(config, agentId, ref) {
	const { directRef, typeId } = parseModelRefInput(ref);
	if (directRef) {
		if (!parseModelRef(directRef)) throw new Error(`model ref must be provider/model format (got '${directRef}')`);
		return directRef;
	}
	if (typeId) {
		const resolved = resolveTypedModelRef(config, agentId, typeId);
		if (resolved) return resolved;
		const available = [...resolveEffectiveTypedModels(config, agentId).keys()];
		const hint = available.length > 0 ? ` (available: ${available.join(", ")})` : " (none configured)";
		throw new Error(`Unknown typed model id '${typeId}' for agent '${agentId}'${hint}`);
	}
	throw new Error("model ref must not be empty");
}
//#endregion
export { mergeTypedModels, resolveEffectiveTypedModels, resolveModelRef, resolveTypedModelRef };

//# sourceMappingURL=agent-typed-models.js.map