import { r as formatErrorMessage } from "./errors-CGMew-1w.js";
import { i as normalizeLowercaseStringOrEmpty, s as normalizeOptionalString } from "./string-coerce-CIT1-v_a.js";
import { r as logVerbose } from "./globals-D7B2Z7Cq.js";
import { n as getAcpSessionManager } from "./manager-C2wmIxL8.js";
import "./session-meta-BtGvKgGv.js";
import { c as resolveConfiguredAcpBindingSpecFromRecord, i as resolveConfiguredBindingRecordBySessionKey, l as toResolvedConfiguredAcpBinding, o as buildConfiguredAcpSessionKey, r as resolveConfiguredBindingRecord } from "./binding-registry-BKwUHVhC.js";
//#region src/acp/persistent-bindings.resolve.ts
function resolveConfiguredAcpBindingRecord(params) {
	const resolved = resolveConfiguredBindingRecord(params);
	return resolved ? toResolvedConfiguredAcpBinding(resolved.record) : null;
}
function resolveConfiguredAcpBindingSpecBySessionKey(params) {
	const resolved = resolveConfiguredBindingRecordBySessionKey(params);
	return resolved ? resolveConfiguredAcpBindingSpecFromRecord(resolved.record) : null;
}
//#endregion
//#region src/acp/persistent-bindings.lifecycle.ts
function sessionMatchesConfiguredBinding(params) {
	if (params.meta.state === "error") return false;
	const desiredAgent = normalizeLowercaseStringOrEmpty(params.spec.acpAgentId ?? params.spec.agentId);
	const currentAgent = normalizeLowercaseStringOrEmpty(params.meta.agent);
	if (!currentAgent || currentAgent !== desiredAgent) return false;
	if (params.meta.mode !== params.spec.mode) return false;
	const desiredBackend = normalizeOptionalString(params.spec.backend) ?? normalizeOptionalString(params.cfg.acp?.backend) ?? "";
	if (desiredBackend) {
		const currentBackend = (params.meta.backend ?? "").trim();
		if (!currentBackend || currentBackend !== desiredBackend) return false;
	}
	const desiredCwd = normalizeOptionalString(params.spec.cwd);
	if (desiredCwd !== void 0) {
		if (desiredCwd !== (params.meta.runtimeOptions?.cwd ?? params.meta.cwd ?? "").trim()) return false;
	}
	return true;
}
async function ensureConfiguredAcpBindingSession(params) {
	const sessionKey = buildConfiguredAcpSessionKey(params.spec);
	const acpManager = getAcpSessionManager();
	try {
		const resolution = acpManager.resolveSession({
			cfg: params.cfg,
			sessionKey
		});
		if (resolution.kind === "ready" && sessionMatchesConfiguredBinding({
			cfg: params.cfg,
			spec: params.spec,
			meta: resolution.meta
		})) return {
			ok: true,
			sessionKey
		};
		if (resolution.kind !== "none") await acpManager.closeSession({
			cfg: params.cfg,
			sessionKey,
			reason: "config-binding-reconfigure",
			clearMeta: false,
			allowBackendUnavailable: true,
			requireAcpSession: false
		});
		await acpManager.initializeSession({
			cfg: params.cfg,
			sessionKey,
			agent: params.spec.acpAgentId ?? params.spec.agentId,
			mode: params.spec.mode,
			cwd: params.spec.cwd,
			backendId: params.spec.backend
		});
		return {
			ok: true,
			sessionKey
		};
	} catch (error) {
		const message = formatErrorMessage(error);
		logVerbose(`acp-configured-binding: failed ensuring ${params.spec.channel}:${params.spec.accountId}:${params.spec.conversationId} -> ${sessionKey}: ${message}`);
		return {
			ok: false,
			sessionKey,
			error: message
		};
	}
}
async function ensureConfiguredAcpBindingReady(params) {
	if (!params.configuredBinding) return { ok: true };
	const ensured = await ensureConfiguredAcpBindingSession({
		cfg: params.cfg,
		spec: params.configuredBinding.spec
	});
	if (ensured.ok) return { ok: true };
	return {
		ok: false,
		error: ensured.error ?? "unknown error"
	};
}
//#endregion
export { resolveConfiguredAcpBindingSpecBySessionKey as i, ensureConfiguredAcpBindingSession as n, resolveConfiguredAcpBindingRecord as r, ensureConfiguredAcpBindingReady as t };
