import type { ReviewComment } from "./types";

/**
 * Build a Map keyed by `"${filePath}:${lineNumber}:${lineType}"` for line-level
 * comments, or `"${filePath}:file"` for file-level comments (lineNumber === null).
 *
 * The compound key (filePath + lineNumber + lineType) is required because
 * added and removed lines can share the same numeric position in a replacement
 * hunk (e.g., old line 10 removed, new line 10 added).
 */
export function buildCommentIndex(
  comments: ReviewComment[],
): Map<string, ReviewComment[]> {
  const map = new Map<string, ReviewComment[]>();
  for (const comment of comments) {
    const key =
      comment.lineNumber != null
        ? `${comment.filePath}:${comment.lineNumber}:${comment.lineType}`
        : `${comment.filePath}:file`;
    const existing = map.get(key);
    if (existing) existing.push(comment);
    else map.set(key, [comment]);
  }
  return map;
}
