import { DEFAULT_IGNORED_COMMENT_LOGINS } from "@/core/constants.js";

export interface ShouldIgnoreCommentParams {
  /** Comment author's GitHub login, or null if GitHub did not return one. */
  login: string | null;
  /** The bot's own login — comments it authored are always ignored. */
  botUser: string;
  /** Extra logins to ignore (e.g. cursor[bot], hu-reviewer[bot]). */
  ignoredLogins: ReadonlySet<string> | readonly string[];
}

/**
 * Pure predicate: returns true when the comment should NOT trigger a reply.
 *
 * - Bot's own comments -> ignored (avoids infinite loops).
 * - Any login in `ignoredLogins` -> ignored (config-driven allow-list).
 * - null / unknown author -> NOT ignored (safer to attempt a reply than drop silently).
 */
export function shouldIgnoreComment(params: ShouldIgnoreCommentParams): boolean {
  const { login, botUser, ignoredLogins } = params;
  if (login === null) return false;
  if (login === botUser) return true;
  const set = ignoredLogins instanceof Set ? ignoredLogins : new Set(ignoredLogins);
  return set.has(login);
}

/**
 * Parses the IGNORED_COMMENT_LOGINS env var.
 *
 * - undefined / empty string -> DEFAULT_IGNORED_COMMENT_LOGINS (as a fresh array).
 * - non-empty string -> comma-split, trimmed, empty segments dropped.
 *
 * The env var REPLACES the default wholesale — by design. Callers that need
 * to extend the default should read DEFAULT_IGNORED_COMMENT_LOGINS directly.
 */
export function parseIgnoredCommentLogins(raw: string | undefined): string[] {
  if (raw === undefined || raw === "") {
    return [...DEFAULT_IGNORED_COMMENT_LOGINS];
  }
  return raw
    .split(",")
    .map((s) => s.trim())
    .filter(Boolean);
}
