import { createExecutionResources, resolveCommandFlags } from "../../shared.js";
import { attachJob, createLineBufferedLogWriter, createRuntimeState, parseSince, resolveJob, streamJobLog, syncJob } from "./shared.js";
export function registerRuntimeJobsAttachCommand(jobs, root, container) {
    jobs
        .command("attach")
        .description("Attach to a detached runtime job log stream.")
        .argument("[jobId]", "Detached job id")
        .option("--since <duration>", "Only include logs from recently modified log files")
        .option("--sync-on-exit", "Download the workspace after the job exits")
        .option("--force-sync", "Overwrite local files when syncing")
        .action(async (jobId, options) => {
        await executeRuntimeJobsAttach(root, container, jobId, options);
    });
}
async function executeRuntimeJobsAttach(root, container, jobId, options) {
    const flags = resolveCommandFlags(root);
    const resources = createExecutionResources(container, flags, "runtime:jobs:attach");
    const state = createRuntimeState(container);
    const entry = await resolveJob(state, jobId, "running");
    const since = parseSince(options.since);
    if (flags.dryRun) {
        const syncDetail = options.syncOnExit === true ? " and sync its workspace on exit" : "";
        resources.logger.dryRun(`Dry run: would attach to runtime job ${entry.id}${syncDetail}.`);
        return;
    }
    const { handle } = await attachJob(entry);
    const logWriter = createLineBufferedLogWriter((line) => {
        resources.logger.info(line);
    });
    await streamJobLog(handle, {
        since,
        follow: true,
        write(chunk) {
            logWriter.write(chunk);
        },
        onDetach() {
            logWriter.flush();
            resources.logger.info("detaching (job continues running)");
        }
    });
    logWriter.flush();
    if (options.syncOnExit === true && (await handle.status()) !== "running") {
        await syncJob(entry, { forceSync: options.forceSync === true, close: false });
    }
}
//# sourceMappingURL=attach.js.map