import { DREAMING_CRON_NAME, DREAMING_LIGHT_CRON_NAME, DREAMING_LIGHT_SWEEP_TOKEN, DREAMING_REM_CRON_NAME, DREAMING_REM_SWEEP_TOKEN, DREAMING_SWEEP_TOKEN } from "../memory/dreaming/constants.js";
import { resolveDreamingConfig } from "../memory/dreaming/config.js";
//#region src/agent/service/reconcile-dreaming-cron.ts
const DREAMING_SWEEP_TOKENS = new Set([
	DREAMING_LIGHT_SWEEP_TOKEN,
	DREAMING_SWEEP_TOKEN,
	DREAMING_REM_SWEEP_TOKEN
]);
function dreamingSweepTokenFromJob(job) {
	if (job.payload?.kind !== "agentTurn") return;
	const msg = job.payload.message;
	return typeof msg === "string" && DREAMING_SWEEP_TOKENS.has(msg) ? msg : void 0;
}
/**
* Reconcile managed Dreaming cron jobs against the current effective config.
*/
async function reconcileManagedDreamingCronJobs(cron, effectiveConfig) {
	const dreaming = resolveDreamingConfig(effectiveConfig);
	/** Match by sweep token — job names omit `[managed-by=…]`, so name-only matching never saw these jobs and re-added them every reconcile. */
	const managed = (await cron.listJobs()).filter((job) => dreamingSweepTokenFromJob(job) !== void 0);
	const phaseSpecs = [
		{
			cronName: DREAMING_LIGHT_CRON_NAME,
			token: DREAMING_LIGHT_SWEEP_TOKEN,
			schedule: dreaming.phases.light.cron,
			phaseEnabled: dreaming.phases.light.enabled
		},
		{
			cronName: DREAMING_CRON_NAME,
			token: DREAMING_SWEEP_TOKEN,
			schedule: dreaming.phases.deep.cron,
			phaseEnabled: dreaming.phases.deep.enabled
		},
		{
			cronName: DREAMING_REM_CRON_NAME,
			token: DREAMING_REM_SWEEP_TOKEN,
			schedule: dreaming.phases.rem.cron,
			phaseEnabled: dreaming.phases.rem.enabled
		}
	];
	if (!dreaming.enabled) {
		for (const job of managed) await cron.removeJob(job.id).catch(() => {});
		return;
	}
	for (const spec of phaseSpecs) {
		const phaseJobs = managed.filter((job) => dreamingSweepTokenFromJob(job) === spec.token);
		if (!spec.phaseEnabled) {
			for (const job of phaseJobs) await cron.removeJob(job.id).catch(() => {});
			continue;
		}
		const desiredPayload = {
			kind: "agentTurn",
			message: spec.token
		};
		if (phaseJobs.length === 0) {
			await cron.addJob(spec.schedule, {
				name: spec.cronName,
				timezone: dreaming.timezone,
				sessionTarget: "isolated",
				payload: desiredPayload,
				enabled: true
			});
			continue;
		}
		const primary = phaseJobs[0];
		for (const dup of phaseJobs.slice(1)) await cron.removeJob(dup.id).catch(() => {});
		const payloadMessage = primary.payload?.kind === "agentTurn" ? primary.payload.message : primary.payload?.text;
		if (primary.schedule !== spec.schedule || (dreaming.timezone ?? null) !== (primary.timezone ?? null) || primary.sessionTarget !== "isolated" || payloadMessage !== spec.token || primary.enabled !== true || primary.name !== spec.cronName) await cron.updateJob(primary.id, {
			schedule: spec.schedule,
			timezone: dreaming.timezone ?? void 0,
			sessionTarget: "isolated",
			name: spec.cronName,
			payload: desiredPayload,
			enabled: true
		});
	}
}
//#endregion
export { reconcileManagedDreamingCronJobs };

//# sourceMappingURL=reconcile-dreaming-cron.js.map