import { DEFAULT_HEARTBEAT_INTERVAL_MS } from "../config/types.js";
import { takeScreenshot } from "../tools/screenshot.js";
import { getCalendarSummary } from "../calendar/scheduler.js";
import { checkForUpdate, recordUpdateNotification, } from "../utils/version-check.js";
import { logger } from "../logger.js";
/**
 * Gather screenshot, calendar, and (optionally) version-check data in
 * parallel. Failures in individual sources are logged and swallowed so
 * the agent can still proceed with whatever data is available.
 *
 * Calendar events are also monitored by CalendarInterruptPoller for
 * mid-task interrupt notifications, but the wake-cycle fetch here ensures
 * the agent always has calendar awareness on every wake.
 */
export async function gatherWakeContext(config, doVersionCheck, skipScreenshot = false, heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS) {
    const [screenshotResult, calendarResult, versionResult] = await Promise.allSettled([
        (async () => {
            if (skipScreenshot) {
                logger.debug("Skipping screenshot (consecutive errors)");
                return undefined;
            }
            const t0 = Date.now();
            const { base64 } = await takeScreenshot();
            logger.debug(`Screenshot captured in ${Date.now() - t0}ms`);
            return { base64 };
        })(),
        (async () => {
            const t0 = Date.now();
            const summary = await getCalendarSummary(config, heartbeatIntervalMs);
            logger.debug(`Calendar fetched in ${Date.now() - t0}ms`);
            return summary;
        })(),
        (async () => {
            if (!doVersionCheck)
                return undefined;
            const t0 = Date.now();
            const result = await checkForUpdate();
            logger.debug(`Version check completed in ${Date.now() - t0}ms`);
            if (result.hasUpdate) {
                recordUpdateNotification(result.latestVersion);
                logger.system(`Update available: ${result.currentVersion} → ${result.latestVersion}`);
            }
            return result;
        })(),
    ]);
    const screenshot = screenshotResult.status === "fulfilled" ? screenshotResult.value : undefined;
    if (screenshotResult.status === "rejected") {
        logger.warn(`Screenshot failed: ${screenshotResult.reason instanceof Error ? screenshotResult.reason.message : String(screenshotResult.reason)}`);
    }
    if (screenshot?.base64) {
        logger.screenshot(screenshot.base64);
    }
    // TODO: Maybe we should fetch unread emails here?
    const calendarSummary = calendarResult.status === "fulfilled" ? calendarResult.value : "";
    if (calendarResult.status === "rejected") {
        logger.warn(`Calendar fetch failed: ${calendarResult.reason instanceof Error ? calendarResult.reason.message : String(calendarResult.reason)}`);
    }
    const versionUpdate = versionResult.status === "fulfilled" ? versionResult.value : undefined;
    if (versionResult.status === "rejected") {
        logger.debug(`Version check failed: ${versionResult.reason instanceof Error ? versionResult.reason.message : String(versionResult.reason)}`);
    }
    return { screenshot, calendarSummary, versionUpdate };
}
//# sourceMappingURL=gather-context.js.map