import { i as normalizeLowercaseStringOrEmpty, o as normalizeOptionalLowercaseString, r as lowercasePreservingWhitespace, u as normalizeStringifiedOptionalString } from "./string-coerce-CIT1-v_a.js";
import { m as resolveDefaultAgentId, p as resolveAgentWorkspaceDir } from "./agent-scope-BA9YeMqS.js";
import { n as asNullableRecord } from "./record-coerce-L40G7Uo_.js";
import path from "node:path";
//#region src/memory-host-sdk/consolidation.ts
const DEFAULT_MEMORY_CONSOLIDATION_ENABLED = true;
const DEFAULT_MEMORY_CONSOLIDATION_CRON = "0 3 * * *";
const DEFAULT_MEMORY_CONSOLIDATION_CONCURRENCY = 2;
const DEFAULT_MEMORY_CONSOLIDATION_BATCH_SIZE = 16e3;
const DEFAULT_MEMORY_CONSOLIDATION_LOOKBACK_DAYS = 7;
const DEFAULT_MEMORY_CONSOLIDATION_VERBOSE_LOGGING = false;
const DEFAULT_MEMORY_CONSOLIDATION_STORAGE_MODE = "separate";
function normalizeTrimmedString(value) {
	if (typeof value !== "string") return;
	const trimmed = value.trim();
	return trimmed.length > 0 ? trimmed : void 0;
}
function normalizeNonNegativeInt(value, fallback) {
	const normalized = normalizeStringifiedOptionalString(value);
	if (typeof value === "string" && !normalized) return fallback;
	const num = typeof value === "string" ? Number(normalized) : Number(value);
	if (!Number.isFinite(num)) return fallback;
	const floored = Math.floor(num);
	if (floored < 0) return fallback;
	return floored;
}
function normalizeBoolean(value, fallback) {
	if (typeof value === "boolean") return value;
	if (typeof value === "string") {
		const normalized = normalizeLowercaseStringOrEmpty(value);
		if (normalized === "true") return true;
		if (normalized === "false") return false;
	}
	return fallback;
}
function normalizePathForComparison(input) {
	const normalized = path.resolve(input);
	return process.platform === "win32" ? lowercasePreservingWhitespace(normalized) : normalized;
}
function resolveConsolidationConfig(params) {
	const consolidation = asNullableRecord(params.pluginConfig?.consolidation);
	const cron = normalizeTrimmedString(consolidation?.cron) ?? "0 3 * * *";
	const timezone = normalizeTrimmedString(consolidation?.timezone) ?? normalizeTrimmedString(params.cfg?.agents?.defaults?.userTimezone) ?? void 0;
	return {
		enabled: normalizeBoolean(consolidation?.enabled, true),
		cron,
		...timezone ? { timezone } : {},
		verboseLogging: normalizeBoolean(consolidation?.verboseLogging, false),
		concurrency: normalizeNonNegativeInt(consolidation?.concurrency, 2),
		batchSize: normalizeNonNegativeInt(consolidation?.batchSize, DEFAULT_MEMORY_CONSOLIDATION_BATCH_SIZE),
		lookbackDays: normalizeNonNegativeInt(consolidation?.lookbackDays, 7)
	};
}
function resolveConsolidationWorkspaces(cfg) {
	const configured = Array.isArray(cfg.agents?.list) ? cfg.agents.list : [];
	const agentIds = [];
	const seenAgents = /* @__PURE__ */ new Set();
	for (const entry of configured) {
		if (!entry || typeof entry !== "object" || typeof entry.id !== "string") continue;
		const id = normalizeOptionalLowercaseString(entry.id);
		if (!id || seenAgents.has(id)) continue;
		seenAgents.add(id);
		agentIds.push(id);
	}
	if (agentIds.length === 0) agentIds.push(resolveDefaultAgentId(cfg));
	const byWorkspace = /* @__PURE__ */ new Map();
	for (const agentId of agentIds) {
		const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId)?.trim();
		if (!workspaceDir) continue;
		const key = normalizePathForComparison(workspaceDir);
		const existing = byWorkspace.get(key);
		if (existing) {
			existing.agentIds.push(agentId);
			continue;
		}
		byWorkspace.set(key, {
			workspaceDir,
			agentIds: [agentId]
		});
	}
	return [...byWorkspace.values()];
}
//#endregion
export { DEFAULT_MEMORY_CONSOLIDATION_LOOKBACK_DAYS as a, resolveConsolidationConfig as c, DEFAULT_MEMORY_CONSOLIDATION_ENABLED as i, resolveConsolidationWorkspaces as l, DEFAULT_MEMORY_CONSOLIDATION_CONCURRENCY as n, DEFAULT_MEMORY_CONSOLIDATION_STORAGE_MODE as o, DEFAULT_MEMORY_CONSOLIDATION_CRON as r, DEFAULT_MEMORY_CONSOLIDATION_VERBOSE_LOGGING as s, DEFAULT_MEMORY_CONSOLIDATION_BATCH_SIZE as t };
