import runDbClient_default from "../../../../data/db/runDbClient.js";
import { getLogger as getLogger$1 } from "../../../../logger.js";
import { dispatchDueTriggers } from "../../services/triggerDispatcher.js";
import { getSchedulerState, upsertSchedulerState } from "@inkeep/agents-core";

//#region src/domains/run/workflow/steps/schedulerSteps.ts
const logger = getLogger$1("scheduler-steps");
async function registerSchedulerStep(params) {
	"use step";
	await upsertSchedulerState(runDbClient_default)({ currentRunId: params.runId });
	logger.info({ runId: params.runId }, "Scheduler registered");
}
async function checkSchedulerCurrentStep(params) {
	"use step";
	const state = await getSchedulerState(runDbClient_default)();
	if (!state?.currentRunId) return false;
	return state.currentRunId === params.runId;
}
async function msUntilNextMinuteStep() {
	"use step";
	const ms = 6e4 - Date.now() % 6e4;
	return Math.max(ms, 1e3);
}
async function dispatchDueTriggersStep() {
	"use step";
	const result = await dispatchDueTriggers();
	if (result.dispatched > 0) logger.info({ dispatched: result.dispatched }, "Dispatch tick completed");
}

//#endregion
export { checkSchedulerCurrentStep, dispatchDueTriggersStep, msUntilNextMinuteStep, registerSchedulerStep };