/**
 * ConflictAnalyzerService — read-only conflict provenance analysis over patch history.
 *
 * Orchestrates the pipeline: ConflictFrameLoader → ConflictCandidateCollector →
 * ConflictTraceAssembler → ConflictAnalysis.
 *
 * @module domain/services/strand/ConflictAnalyzerService
 */
import ConflictAnalysis from "../../types/conflict/ConflictAnalysis.js";
import ConflictAnalysisRequest from "./ConflictAnalysisRequest.js";
import { resolveAnalysisContext, attachReceipts, ScanWindow, CONFLICT_ANALYSIS_VERSION, } from "./ConflictFrameLoader.js";
import ConflictPipelineContext from "./ConflictPipelineContext.js";
import { ConflictCandidateCollector } from "./ConflictCandidateCollector.js";
import { groupCandidates, buildConflictTraces, filterTraces, buildAnalysisSnapshotHash, buildEmptySnapshotHash, } from "./ConflictTraceAssembler.js";
export { CONFLICT_ANALYSIS_VERSION };
/**
 * ConflictAnalyzerService analyzes read-only patch history for conflict traces.
 */
export class ConflictAnalyzerService {
    _pipelineContext;
    /**
     * Initializes the analyzer with a warp runtime graph instance.
     */
    constructor({ graph }) {
        this._pipelineContext = new ConflictPipelineContext({ graph });
    }
    /**
     * Performs a full conflict analysis over the patch history.
     */
    async analyze(options) {
        const request = ConflictAnalysisRequest.from(options);
        const diagnostics = [];
        const context = this._pipelineContext;
        const { patchFrames, resolvedCoordinate } = await resolveAnalysisContext(context, request);
        if (patchFrames.length === 0) {
            return await this._emptyResult(resolvedCoordinate, request, diagnostics);
        }
        attachReceipts(patchFrames);
        const scanWindow = new ScanWindow({
            patchFrames, maxPatches: request.maxPatches, lamportCeiling: request.lamportCeiling, diagnostics,
        });
        const collector = await ConflictCandidateCollector.collect(context, {
            patchFrames, scannedPatchShas: scanWindow.scannedPatchShas, diagnostics,
        });
        const traces = await buildConflictTraces(context, {
            grouped: groupCandidates(collector.candidates).values(), evidence: request.evidence, resolvedCoordinate,
        });
        const conflicts = filterTraces(traces, request);
        const analysisSnapshotHash = await buildAnalysisSnapshotHash(context, {
            resolvedCoordinate, request, truncated: scanWindow.truncated, diagnostics, traces: conflicts,
        });
        return new ConflictAnalysis({
            analysisVersion: CONFLICT_ANALYSIS_VERSION, resolvedCoordinate,
            analysisSnapshotHash, diagnostics, conflicts,
        });
    }
    async _emptyResult(resolvedCoordinate, request, diagnostics) {
        const context = this._pipelineContext;
        return new ConflictAnalysis({
            analysisVersion: CONFLICT_ANALYSIS_VERSION, resolvedCoordinate,
            analysisSnapshotHash: await buildEmptySnapshotHash(context, { resolvedCoordinate, request }),
            diagnostics, conflicts: [],
        });
    }
}
export default ConflictAnalyzerService;
