/**
 * MonitorEngine lifecycle as a `DaemonService` (RUSH-3193 P2).
 *
 * Wraps the `MonitorEngine` (event-triggered watchers) under the
 * `ServiceSupervisor` contract. The engine can own a timer when embedded by a
 * foreground caller, but the daemon starts it in external-scheduler mode so
 * every evaluation cycle receives the supervisor's deadline, health, and
 * circuit-breaker semantics.
 *
 * `getEngine()` exposes the underlying `MonitorEngine` so `daemon.ts`'s
 * SIGHUP handler can call `engine.reload()` when needed.
 */
import { BasePeriodicService } from './service.js';
import { MONITOR_ENGINE_TICK_MS, MonitorEngine } from '../monitors/engine.js';
export class MonitorEngineService extends BasePeriodicService {
    id = 'monitors';
    intervalMs = MONITOR_ENGINE_TICK_MS;
    deadlineMs = 2 * 60_000;
    engine = null;
    /** Returns the live engine after `start()`, or `null` before/after. */
    getEngine() {
        return this.engine;
    }
    async onStart(ctx) {
        this.engine = new MonitorEngine((level, message) => ctx.log(level, message));
        this.engine.start({ externalScheduler: true });
    }
    async onStop() {
        this.engine?.stop();
        this.engine = null;
    }
    async onTick(_ctx) {
        if (!this.engine)
            throw new Error('monitor engine tick requested before start');
        await this.engine.tick();
    }
}
