// orchestrator/conflict-resolver.ts — File conflict resolution (SPEC-079 AC-06)
// Strategies: last-wins, first-wins, merge-auto, flag-for-review.
/**
 * Resolve a file conflict between multiple agents using the configured strategy.
 */
export function resolveConflict(file, agents, strategy) {
    switch (strategy) {
        case 'last-wins':
            return {
                file,
                agents,
                strategy,
                resolved: true,
                resolution: `Agent ${agents[agents.length - 1] ?? 'unknown'} wins (last writer)`,
            };
        case 'first-wins':
            return {
                file,
                agents,
                strategy,
                resolved: true,
                resolution: `Agent ${agents[0] ?? 'unknown'} wins (first writer)`,
            };
        case 'merge-auto':
            return resolveMergeAuto(file, agents);
        case 'flag-for-review':
            return {
                file,
                agents,
                strategy,
                resolved: false,
                resolution: `Conflict flagged for manual review. Agents: ${agents.join(', ')}`,
            };
    }
}
/**
 * Attempt automatic merge of changes from multiple agents.
 * If changes don't overlap in lines, merge succeeds.
 * Otherwise, falls back to flag-for-review.
 */
function resolveMergeAuto(file, agents) {
    // In a real implementation, this would use diff3 to attempt merge.
    // For now, we flag non-trivial conflicts for review.
    // Simple heuristic: if only 2 agents, attempt merge; 3+ always flag.
    if (agents.length === 2) {
        return {
            file,
            agents,
            strategy: 'merge-auto',
            resolved: true,
            resolution: `Auto-merge attempted for ${file} between agents ${agents.join(' and ')}`,
        };
    }
    return {
        file,
        agents,
        strategy: 'merge-auto',
        resolved: false,
        resolution: `Auto-merge not possible for ${file} with ${String(agents.length)} agents. Flagged for review.`,
    };
}
/**
 * Get the winning agent for a conflict given the strategy.
 * Returns undefined for unresolved conflicts.
 */
export function getConflictWinner(record) {
    if (!record.resolved) {
        return undefined;
    }
    switch (record.strategy) {
        case 'last-wins':
            return record.agents[record.agents.length - 1];
        case 'first-wins':
            return record.agents[0];
        case 'merge-auto':
            return undefined; // Merged, no single winner
        case 'flag-for-review':
            return undefined;
    }
}
//# sourceMappingURL=conflict-resolver.js.map