import { EventLog } from './events/append.js';
import { scanColdWorkflowRuns, } from './cold-scan.js';
/**
 * Daemon cold-start attach path: scan persisted runs owned by this Lark app,
 * attach fan-out watchers, then kick the runtime loop so recovery can settle
 * dangling effects / crashed workers.
 */
export async function attachColdWorkflowRunsForDaemon(opts) {
    const runs = await scanColdWorkflowRuns({
        runsDir: opts.runsDir,
        ownerLarkAppId: opts.ownerLarkAppId,
        onSkip: opts.onSkip,
    });
    const attached = [];
    for (const run of runs) {
        if (opts.isAttached?.(run.runId))
            continue;
        const log = new EventLog(run.runId, opts.runsDir);
        const ctx = opts.makeContext(run, log);
        const watcher = opts.attachWatcher(run.runId, ctx);
        try {
            await watcher.ready;
        }
        catch (err) {
            opts.onSkip?.(run.runId, `watcher-start-failed: ${err instanceof Error ? err.message : String(err)}`);
            continue;
        }
        attached.push(run.runId);
        opts.onAttached?.(run);
        const driven = opts.driveRun(run.runId, ctx);
        if (opts.awaitDrive) {
            await driven;
        }
        else {
            driven.catch((err) => opts.onDriveError?.(run.runId, err));
        }
    }
    return { discovered: runs.length, attached };
}
//# sourceMappingURL=cold-attach.js.map