import { DEFAULT_DEEP_CRON, DEFAULT_LIGHT_CRON, DEFAULT_REM_CRON } from "./constants.js";
//#region src/agent/memory/dreaming/config.ts
function clampScore(value, fallback) {
	if (!Number.isFinite(value)) return fallback;
	return Math.max(0, Math.min(1, value));
}
function toPositiveInt(value, fallback) {
	const num = typeof value === "string" ? Number(value) : Number(value);
	if (!Number.isFinite(num)) return fallback;
	const floored = Math.floor(num);
	return floored > 0 ? floored : fallback;
}
function toNonNegInt(value, fallback) {
	const num = typeof value === "string" ? Number(value) : Number(value);
	if (!Number.isFinite(num)) return fallback;
	const floored = Math.floor(num);
	return floored >= 0 ? floored : fallback;
}
function trimmedStringOr(value, fallback) {
	return typeof value === "string" && value.trim() ? value.trim() : fallback;
}
function optionalTrimmedString(value) {
	return typeof value === "string" && value.trim() ? value.trim() : void 0;
}
/**
* Resolve the full three-phase dreaming config from the app Config.
*
* Reads through `any` so it can ship independently of the schema evolution.
* Expected shape: `agents.defaults.memory.dreaming.{ enabled, frequency, timezone, phases.{light,deep,rem}.* }`
*/
function resolveDreamingConfig(cfg) {
	const dreaming = cfg?.agents?.defaults?.memory?.dreaming;
	const enabled = dreaming?.enabled === true;
	const frequency = trimmedStringOr(dreaming?.frequency, DEFAULT_DEEP_CRON);
	const timezone = optionalTrimmedString(dreaming?.timezone);
	const lightRaw = dreaming?.phases?.light ?? {};
	const light = {
		enabled: enabled && lightRaw?.enabled !== false,
		cron: trimmedStringOr(lightRaw?.cron, DEFAULT_LIGHT_CRON),
		lookbackDays: toPositiveInt(lightRaw?.lookbackDays, 2),
		limit: toNonNegInt(lightRaw?.limit, 100),
		dedupeSimilarity: clampScore(Number(lightRaw?.dedupeSimilarity), .9)
	};
	const deepRaw = dreaming?.phases?.deep ?? {};
	const deep = {
		enabled: enabled && deepRaw?.enabled !== false,
		cron: trimmedStringOr(deepRaw?.cron, frequency),
		minScore: clampScore(Number(deepRaw?.minScore), .8),
		minRecallCount: toPositiveInt(deepRaw?.minRecallCount, 3),
		minUniqueQueries: toPositiveInt(deepRaw?.minUniqueQueries, 3),
		limit: toNonNegInt(deepRaw?.limit, 10),
		recencyHalfLifeDays: toPositiveInt(deepRaw?.recencyHalfLifeDays, 14),
		maxAgeDays: toPositiveInt(deepRaw?.maxAgeDays, 30)
	};
	const remRaw = dreaming?.phases?.rem ?? {};
	const rem = {
		enabled: enabled && remRaw?.enabled !== false,
		cron: trimmedStringOr(remRaw?.cron, DEFAULT_REM_CRON),
		lookbackDays: toPositiveInt(remRaw?.lookbackDays, 7),
		limit: toNonNegInt(remRaw?.limit, 10),
		minPatternStrength: clampScore(Number(remRaw?.minPatternStrength), .75)
	};
	return {
		enabled,
		frequency,
		...timezone ? { timezone } : {},
		phases: {
			light,
			deep,
			rem
		},
		deep
	};
}
/** Get the cron expression for a specific phase from resolved config. */
function getPhaseCron(config, phase) {
	return config.phases[phase].cron;
}
/** Check if a specific phase is enabled (global + phase-level). */
function isPhaseEnabled(config, phase) {
	return config.enabled && config.phases[phase].enabled;
}
//#endregion
export { getPhaseCron, isPhaseEnabled, resolveDreamingConfig };

//# sourceMappingURL=config.js.map