"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HealthCheck = void 0;
const defaultTrigger_1 = require("../trigger/defaultTrigger");
const util_1 = require("../util");
/**
 * A periodic health check that reports daemon readiness to the StartOS UI.
 *
 * Polls at an interval controlled by a {@link Trigger}, reporting results as
 * "starting" (during the grace period), "success", or "failure". Automatically
 * pauses when the daemon is stopped and resumes when restarted.
 */
class HealthCheck extends util_1.Drop {
    constructor(effects, o) {
        super();
        this.started = null;
        this.setStarted = (started) => {
            this.started = started;
        };
        this.exited = false;
        this.exit = () => {
            this.exited = true;
        };
        this.currentValue = {};
        this.promise = Promise.resolve().then(async () => {
            const getCurrentValue = () => this.currentValue;
            const gracePeriod = o.gracePeriod ?? 10_000;
            const trigger = (o.trigger ?? defaultTrigger_1.defaultTrigger)(getCurrentValue);
            const checkStarted = () => [
                this.started,
                new Promise((resolve) => {
                    this.setStarted = (started) => {
                        this.started = started;
                        resolve();
                    };
                    this.exit = () => {
                        this.exited = true;
                        resolve();
                    };
                }),
            ];
            let triggered = false;
            while (!this.exited) {
                const [started, changed] = checkStarted();
                let race = [
                    changed,
                ];
                if (started) {
                    race = [...race, trigger.next()];
                    if (triggered) {
                        try {
                            let { result, message } = await o.fn();
                            if (result === 'failure' &&
                                performance.now() - started <= gracePeriod)
                                result = 'starting';
                            await effects.setHealth({
                                name: o.name,
                                id: o.id,
                                result,
                                message: message || '',
                            });
                            this.currentValue.lastResult = result;
                        }
                        catch (e) {
                            await effects.setHealth({
                                name: o.name,
                                id: o.id,
                                result: performance.now() - started <= gracePeriod
                                    ? 'starting'
                                    : 'failure',
                                message: asMessage(e) || '',
                            });
                            this.currentValue.lastResult = 'failure';
                        }
                    }
                }
                else
                    triggered = false;
                const raced = await Promise.race(race);
                if (raced) {
                    if (raced.done)
                        break;
                    triggered = true;
                }
            }
        });
    }
    /**
     * Create a new HealthCheck instance and begin its polling loop.
     * @param effects - The effects context for reporting health status
     * @param options - Health check configuration (ID, name, check function, trigger, grace period)
     * @returns A new HealthCheck instance
     */
    static of(effects, options) {
        return new HealthCheck(effects, options);
    }
    /** Signal that the daemon is running, enabling health check polling */
    start() {
        if (this.started)
            return;
        this.setStarted(performance.now());
    }
    /** Signal that the daemon has stopped, pausing health check polling */
    stop() {
        if (!this.started)
            return;
        this.setStarted(null);
    }
    onDrop() {
        this.exit();
    }
}
exports.HealthCheck = HealthCheck;
function asMessage(e) {
    if (typeof e === 'object' && e !== null && 'message' in e)
        return String(e.message);
    const value = String(e);
    if (value.length == null)
        return null;
    return value;
}
//# sourceMappingURL=HealthCheck.js.map