import { PostProcessor } from "../processor-interfaces.js";
import { frameworkLogger } from "../../core/framework-logger.js";
import { IntelligentCommitBatcher, } from "../../orchestrator/intelligent-commit-batcher.js";
let batcherSingleton = null;
function getBatcher() {
    if (!batcherSingleton) {
        batcherSingleton = new IntelligentCommitBatcher();
    }
    return batcherSingleton;
}
export class CommitBatcherProcessor extends PostProcessor {
    name = "commitBatcher";
    priority = 85;
    trackedCount = 0;
    batcher;
    constructor(batcher) {
        super();
        this.batcher = batcher ?? getBatcher();
    }
    async run(context) {
        try {
            const tool = context.tool;
            const filePath = context.filePath;
            const operation = context.operation;
            if (!tool || !["edit", "write", "apply", "create", "delete", "rename"].includes(tool)) {
                return { success: true, batched: false, reason: "Not a file-modifying tool" };
            }
            if (!filePath) {
                return { success: true, batched: false, reason: "No file path" };
            }
            const change = {
                filePath,
                operation: tool,
                changeType: this.inferChangeType(tool, filePath),
                riskLevel: "low",
                linesChanged: 0,
                timestamp: Date.now(),
            };
            const shouldCommit = this.batcher.addChange(change);
            this.trackedCount += 1;
            if (shouldCommit) {
                const committed = await this.batcher.commitBatch();
                this.trackedCount = 0;
                frameworkLogger.log("commit-batcher-processor", "commit-triggered", "info", { committed, filePath, tool });
                return { success: true, batched: true, committed };
            }
            return { success: true, batched: false, pendingCount: this.trackedCount };
        }
        catch (error) {
            frameworkLogger.log("commit-batcher-processor", "error", "error", { error: error instanceof Error ? error.message : String(error) });
            return { success: false, batched: false, error: String(error) };
        }
    }
    inferChangeType(tool, filePath) {
        if (filePath.includes("test"))
            return "test";
        if (filePath.includes("config"))
            return "config";
        if (["edit", "write", "apply"].includes(tool))
            return "refactor";
        if (tool === "create")
            return "feature";
        if (tool === "delete")
            return "refactor";
        return "refactor";
    }
}
export async function executeCommitBatcherProcessor(context) {
    const processor = new CommitBatcherProcessor();
    return processor.execute(context);
}
