import { EXIT_CODES, parseCommandArgs } from "../infrastructure.js";
import { reindexSchema } from "../schemas.js";
import { openGraph, applyCursorCeiling, emitCursorWarning } from "../shared.js";
/**
 * Handles the `reindex` command: forces a full bitmap index rebuild
 * by clearing cached index state and re-materializing.
 */
export default async function handleReindex({ options, args }) {
    parseCommandArgs(args, {}, reindexSchema);
    const { graph, graphName, persistence } = await openGraph(options);
    const cursorInfo = await applyCursorCeiling(graph, persistence, graphName);
    emitCursorWarning(cursorInfo, null);
    // Clear cached index to force full rebuild
    graph.invalidateIndex();
    try {
        await graph.materialize();
    }
    catch (err) {
        return {
            payload: { error: err.message },
            exitCode: EXIT_CODES.INTERNAL,
        };
    }
    return {
        payload: {
            graph: graphName,
            status: 'ok',
            message: 'Index rebuilt successfully',
        },
        exitCode: EXIT_CODES.OK,
    };
}
