import { init_agent_scope, resolveAgentWorkspaceDir } from "../agent/agent-scope.js";
import { getAgentDefaultModelRef, init_schema } from "./schema.js";
import { init_session_key, parseSessionKey } from "../routing/session-key.js";
import { agentExists, getDefaultAgentId, init_resolve_route } from "../routing/resolve-route.js";
//#region src/config/agent-profile.ts
init_schema();
init_agent_scope();
init_resolve_route();
init_session_key();
function mergeModelConfig(base, override) {
	if (override === void 0) return base;
	if (base === void 0) return override;
	return {
		primary: override.primary ?? base.primary,
		fallbacks: override.fallbacks ?? base.fallbacks
	};
}
function primaryAndFallbacksFromModelConfig(raw) {
	const primary = raw?.primary?.trim();
	const fallbacks = Array.isArray(raw?.fallbacks) ? raw.fallbacks.map((s) => s.trim()).filter(Boolean) : [];
	return {
		primary: primary || void 0,
		fallbacks
	};
}
function mergeDisableLists(a, b) {
	const out = /* @__PURE__ */ new Set();
	for (const x of a ?? []) {
		const s = String(x).trim();
		if (s) out.add(s);
	}
	for (const x of b ?? []) {
		const s = String(x).trim();
		if (s) out.add(s);
	}
	return out;
}
/**
* Agent id used for config lookup from a session key (subagent keys → default agent).
*/
function extractProfileAgentId(sessionKey, config) {
	const parsed = parseSessionKey(sessionKey ?? "");
	if (!parsed) return getDefaultAgentId(config);
	const aid = parsed.agentId;
	if (aid === "subagent" || aid.startsWith("subagent:")) return getDefaultAgentId(config);
	if (!agentExists(aid, config)) return getDefaultAgentId(config);
	return aid.toLowerCase();
}
/**
* Merge `agents.defaults` with the matching `agents.list` entry.
*/
function resolveEffectiveAgentProfile(config, agentId) {
	const defaults = config.agents?.defaults;
	const list = config.agents?.list;
	const entry = Array.isArray(list) ? list.find((a) => a && a.enabled !== false && a.id.toLowerCase() === agentId.toLowerCase()) : void 0;
	const resolvedWorkspacePath = resolveAgentWorkspaceDir(config, agentId);
	const { primary: primaryFromMerged, fallbacks: fallbacksFromMerged } = primaryAndFallbacksFromModelConfig(mergeModelConfig(defaults?.model, entry?.model));
	const globalDefault = getAgentDefaultModelRef(config);
	const primaryModelRef = primaryFromMerged?.trim() || globalDefault;
	const disable = mergeDisableLists(defaults?.tools?.disable, entry?.tools?.disable);
	const params = {
		...defaults?.params,
		...entry?.params
	};
	return {
		agentId: agentId.toLowerCase(),
		resolvedWorkspacePath,
		primaryModelRef,
		fallbacks: fallbacksFromMerged,
		thinkingDefault: entry?.thinkingDefault ?? defaults?.thinkingDefault,
		reasoningDefault: entry?.reasoningDefault ?? defaults?.reasoningDefault,
		verboseDefault: entry?.verboseDefault ?? defaults?.verboseDefault,
		systemPromptOverride: entry?.systemPromptOverride?.trim() ? entry.systemPromptOverride : defaults?.systemPromptOverride?.trim() ? defaults.systemPromptOverride : void 0,
		skillsAllowlist: entry?.skills?.length ? [...entry.skills] : defaults?.skills?.length ? [...defaults.skills] : void 0,
		tools: { disable },
		params
	};
}
function resolveEffectiveAgentProfileForSession(config, sessionKey) {
	return resolveEffectiveAgentProfile(config, extractProfileAgentId(sessionKey, config));
}
//#endregion
export { extractProfileAgentId, resolveAgentWorkspaceDir, resolveEffectiveAgentProfile, resolveEffectiveAgentProfileForSession };

//# sourceMappingURL=agent-profile.js.map