import { init_agent_scope, resolveAgentIdByWorkspacePath, resolveDefaultAgentId } from "../agent/agent-scope.js";
import { init_string_coerce, normalizeLowercaseStringOrEmpty } from "../utils/string-coerce.js";
import { parseAgentSessionKey } from "./session-key-utils.js";
import { buildAgentMainSessionKey, init_agent_session_key, normalizeAgentId, normalizeMainKey } from "./agent-session-key.js";
//#region src/routing/resolve-tui-session-key.ts
init_agent_scope();
init_agent_session_key();
init_string_coerce();
function resolveTuiSessionKey(params) {
	const trimmed = (params.raw ?? "").trim();
	if (!trimmed) {
		if (params.sessionScope === "global") return "global";
		return buildAgentMainSessionKey({
			agentId: params.currentAgentId,
			mainKey: params.sessionMainKey
		});
	}
	if (trimmed === "global" || trimmed === "unknown") return trimmed;
	if (trimmed.startsWith("agent:")) return normalizeLowercaseStringOrEmpty(trimmed);
	return `agent:${params.currentAgentId}:${normalizeLowercaseStringOrEmpty(trimmed)}`;
}
function resolveInitialTuiAgentId(params) {
	const parsed = parseAgentSessionKey((params.initialSessionInput ?? "").trim());
	if (parsed?.agentId) return normalizeAgentId(parsed.agentId);
	const inferredFromWorkspace = resolveAgentIdByWorkspacePath(params.cfg, params.cwd ?? process.cwd());
	if (inferredFromWorkspace) return inferredFromWorkspace;
	return normalizeAgentId(params.fallbackAgentId);
}
/** Resolve TUI startup session key from CLI options and config. */
function resolveTuiStartupSessionKey(params) {
	const sessionScope = params.cfg.session?.scope ?? "per-sender";
	const sessionMainKey = normalizeMainKey(params.cfg.session?.mainKey);
	const agentId = resolveInitialTuiAgentId({
		cfg: params.cfg,
		fallbackAgentId: resolveDefaultAgentId(params.cfg),
		initialSessionInput: (params.sessionOption ?? "").trim(),
		cwd: params.cwd ?? process.cwd()
	});
	return {
		sessionKey: resolveTuiSessionKey({
			raw: params.sessionOption,
			sessionScope,
			currentAgentId: agentId,
			sessionMainKey
		}),
		agentId,
		sessionScope,
		sessionMainKey
	};
}
//#endregion
export { resolveInitialTuiAgentId, resolveTuiSessionKey, resolveTuiStartupSessionKey };

//# sourceMappingURL=resolve-tui-session-key.js.map