/**
 * GitHub Conflict Resolver
 *
 * Field-level conflict detection and resolution between spec state
 * and GitHub issue state. Supports title, status, and AC fields.
 *
 * @module github-conflict-resolver
 */
export class GitHubConflictResolver {
    constructor(config) {
        this.statusResolution = config?.defaultStatusResolution ?? 'github-wins';
        this.contentResolution = config?.defaultContentResolution ?? 'prompt';
        this.acResolution = config?.defaultACResolution ?? 'github-wins';
    }
    /**
     * Detect conflicts between spec and GitHub states.
     */
    detectConflicts(specState, githubState) {
        const conflicts = [];
        // Title conflict
        if (specState.title !== githubState.title) {
            conflicts.push({
                field: 'title',
                specValue: specState.title,
                githubValue: githubState.title,
                defaultResolution: this.contentResolution,
            });
        }
        // Status conflict: map GitHub state to spec status for comparison
        const githubStatus = githubState.state; // 'open' or 'closed'
        const specStatus = specState.status;
        const statusMismatch = this.isStatusConflict(specStatus, githubStatus);
        if (statusMismatch) {
            conflicts.push({
                field: 'status',
                specValue: specStatus,
                githubValue: githubStatus,
                defaultResolution: this.statusResolution,
            });
        }
        // AC conflicts
        const ghAcMap = new Map(githubState.acceptanceCriteria.map(ac => [ac.id, ac.completed]));
        for (const specAc of specState.acceptanceCriteria) {
            const ghCompleted = ghAcMap.get(specAc.id);
            if (ghCompleted !== undefined && specAc.completed !== ghCompleted) {
                conflicts.push({
                    field: `ac:${specAc.id}`,
                    specValue: String(specAc.completed),
                    githubValue: String(ghCompleted),
                    defaultResolution: this.acResolution,
                });
            }
        }
        return conflicts;
    }
    /**
     * Resolve all conflicts using their default resolution strategy.
     */
    resolveConflicts(conflicts) {
        return conflicts.map(c => this.resolveConflict(c));
    }
    /**
     * Resolve a single conflict with optional override.
     */
    resolveConflict(conflict, resolution) {
        const effectiveResolution = resolution ?? conflict.defaultResolution;
        let resolvedValue;
        switch (effectiveResolution) {
            case 'github-wins':
                resolvedValue = conflict.githubValue;
                break;
            case 'spec-wins':
                resolvedValue = conflict.specValue;
                break;
            case 'prompt':
                // Prompt keeps spec value pending user decision
                resolvedValue = conflict.specValue;
                break;
        }
        return {
            field: conflict.field,
            specValue: conflict.specValue,
            githubValue: conflict.githubValue,
            resolution: effectiveResolution,
            resolvedValue,
            resolvedAt: new Date().toISOString(),
        };
    }
    /**
     * Check if spec status and GitHub state represent a conflict.
     */
    isStatusConflict(specStatus, githubState) {
        // "completed" spec + "open" GitHub = conflict
        if (specStatus === 'completed' && githubState === 'open')
            return true;
        // "planned" or "in-progress" spec + "closed" GitHub = conflict
        if (specStatus !== 'completed' && githubState === 'closed')
            return true;
        return false;
    }
}
//# sourceMappingURL=github-conflict-resolver.js.map