import { z } from 'zod';
import { EXIT_CODES, parseCommandArgs, usageError } from "../infrastructure.js";
import { openGraph, readActiveCursor, readCheckpointDate } from "../shared.js";
const CHECKPOINT_OPTIONS = {};
const checkpointSchema = z.object({}).strict();
function checkpointUsage() {
    throw usageError('Usage: warp-graph checkpoint <status|create|sync-coverage>');
}
async function assertNoActiveCursor(persistence, graphName) {
    const cursor = await readActiveCursor(persistence, graphName);
    if (cursor !== null) {
        throw usageError('checkpoint create refuses to run while seek cursor is active; run git warp seek --latest first');
    }
}
async function checkpointStatus(options) {
    const { graph, graphName, persistence } = await openGraph(options);
    const checkpoint = await graph._readCheckpointSha();
    const date = await readCheckpointDate(persistence, checkpoint);
    return {
        payload: { graph: graphName, checkpoint, date },
        exitCode: EXIT_CODES.OK,
    };
}
async function createCheckpoint(options) {
    const { graph, graphName, persistence } = await openGraph(options);
    await assertNoActiveCursor(persistence, graphName);
    await graph.materialize();
    const checkpoint = await graph.createCheckpoint();
    return {
        payload: { graph: graphName, checkpoint, status: 'created' },
        exitCode: EXIT_CODES.OK,
    };
}
async function syncCoverage(options) {
    const { graph, graphName } = await openGraph(options);
    await graph.syncCoverage();
    return {
        payload: { graph: graphName, status: 'coverage-synced' },
        exitCode: EXIT_CODES.OK,
    };
}
export default async function handleCheckpoint({ options, args }) {
    const action = args[0] ?? 'status';
    const rest = args.slice(1);
    parseCommandArgs(rest, CHECKPOINT_OPTIONS, checkpointSchema);
    if (action === 'status') {
        return await checkpointStatus(options);
    }
    if (action === 'create') {
        return await createCheckpoint(options);
    }
    if (action === 'sync-coverage') {
        return await syncCoverage(options);
    }
    return checkpointUsage();
}
