/**
 * Topic alias map for normalizing free-form topic strings to canonical topics.
 *
 * Maps 200+ free-form topic strings found in papers.yaml to 12 canonical topics.
 * Used by normalizeTopicToCanonical() in research-schemas.ts.
 *
 * @module research/topic-aliases
 * @see Issue #1578 — Topic taxonomy normalization
 */

/**
 * Map of free-form topic strings to canonical topics.
 */
export const TOPIC_ALIASES: Readonly<Record<string, string>> = {
  // → consensus
  voting: 'consensus',
  'consensus-voting': 'consensus',
  debate: 'consensus',
  quorum: 'consensus',
  byzantine: 'consensus',
  'anti-conformity': 'consensus',
  'agreement-based': 'consensus',
  'correlation-aware': 'consensus',
  'LLM consensus and multi-agent voting mechanisms': 'consensus',
  'tolerance-parameter': 'consensus',

  // → routing
  'model-routing': 'routing',
  'model routing': 'routing',
  'llm-routing': 'routing',
  cascade: 'routing',
  'cascade-routing': 'routing',
  'cost-optimization': 'routing',
  'cost-reduction': 'routing',
  'model-selection': 'routing',
  topsis: 'routing',
  linucb: 'routing',
  'contextual-bandit': 'routing',
  'quality-constrained': 'routing',
  'budget-constraints': 'routing',
  'unified-routing': 'routing',
  'query-model-matching': 'routing',
  'adaptive model routing and selection optimization': 'routing',
  'multi-model routing optimization': 'routing',
  'Single-head cross-attention for query-model matching': 'routing',
  'performance-cost-tradeoff': 'routing',
  'task-routing': 'routing',
  'difficulty-estimation': 'routing',
  'score-based': 'routing',
  'optimal-weighting': 'routing',
  'hierarchical-filtering': 'routing',
  'preference-optimization': 'routing',
  'preference-data': 'routing',
  dpo: 'routing',
  'cross-attention': 'routing',
  'cma-es': 'routing',
  profile: 'routing',
  prediction: 'routing',
  lightweight: 'routing',
  bayesian: 'routing',
  'quality-diversity': 'routing',
  'resource-optimization': 'routing',
  'routing-latency': 'routing',
  optimization: 'routing',
  'model-merging': 'routing',
  'model-zoo': 'routing',

  // → memory
  'context-management': 'memory',
  'context-compression': 'memory',
  'context-control': 'memory',
  compression: 'memory',
  'kv-cache': 'memory',
  'long-context': 'memory',
  'long-term-memory': 'memory',
  'episodic-memory': 'memory',
  'graph-memory': 'memory',
  'hierarchical-memory': 'memory',
  'agentic-memory': 'memory',
  'lifelong-learning': 'memory',
  'cross-session-learning': 'memory',
  'continual-learning': 'memory',
  'self-editing-memory': 'memory',
  'six-type-memory': 'memory',
  'belief-memory': 'memory',
  'belief-state': 'memory',
  'action-memory': 'memory',
  'memory-evolution': 'memory',
  'virtual-context': 'memory',
  paging: 'memory',
  'prompt-compression': 'memory',
  'learned-compression': 'memory',
  zettelkasten: 'memory',
  'knowledge-distillation': 'memory',
  'history-encoding': 'memory',
  'ictm-tuple': 'memory',
  'latent-space': 'memory',
  'vector-db': 'memory',
  embedding: 'memory',
  'Taxonomy of context management strategies': 'memory',
  'Task-specific context compression': 'memory',
  'Learned compression modules': 'memory',
  rag: 'memory',
  'retrieval augmented generation': 'memory',
  svd: 'memory',
  'svd-mutation': 'memory',
  vae: 'memory',
  knn: 'memory',
  experience: 'memory',
  'state-aware': 'memory',
  'temporal-graph': 'memory',
  versioning: 'memory',

  // → code-generation
  'code-repair': 'code-generation',
  'code-agent': 'code-generation',
  'code-review': 'code-generation',
  'software-engineering': 'code-generation',
  'swe-bench': 'code-generation',
  'self-improvement': 'code-generation',
  'self-evolving': 'code-generation',
  'self-debug': 'code-generation',
  'self-modification': 'code-generation',
  'skill-library': 'code-generation',
  'skill-discovery': 'code-generation',
  'code review benchmarks': 'code-generation',
  'code generation evaluation': 'code-generation',
  'execution-feedback': 'code-generation',

  // → cli-tools
  mcp: 'cli-tools',
  'MCP protocol': 'cli-tools',
  'MCP security': 'cli-tools',
  'MCP Security': 'cli-tools',
  'mcp-applications': 'cli-tools',
  'model-context-protocol': 'cli-tools',
  puppeteer: 'cli-tools',
  streaming: 'cli-tools',
  integration: 'cli-tools',

  // → orchestration
  'multi-agent': 'orchestration',
  'multi-agent-systems': 'orchestration',
  'multi-agent orchestration': 'orchestration',
  'Multi-Agent Orchestration': 'orchestration',
  'multi-agent-orchestration': 'orchestration',
  'multi-agent coordination': 'orchestration',
  'multi-agent-coordination': 'orchestration',
  'multi-agent-worker-dispatch': 'orchestration',
  'multi-agent reliability': 'orchestration',
  'multi-agent-rl': 'orchestration',
  coordination: 'orchestration',
  coordinator: 'orchestration',
  collaboration: 'orchestration',
  'graph-orchestration': 'orchestration',
  'agent architecture': 'orchestration',
  'dynamic-agent-creation': 'orchestration',
  'specialized-agents': 'orchestration',
  'thinker-worker-verifier': 'orchestration',
  'two-agent': 'orchestration',
  'meta-agent': 'orchestration',
  agents: 'orchestration',
  'workflow-optimization': 'orchestration',
  'workflow-generation': 'orchestration',
  'agent-communication': 'orchestration',
  'inter-agent': 'orchestration',
  'gvu-operator': 'orchestration',
  'rubber-duck': 'orchestration',
  'parallel-exploration': 'orchestration',
  kubernetes: 'orchestration',
  'scale-to-zero': 'orchestration',
  edge: 'orchestration',
  llm: 'orchestration',
  multimodal: 'orchestration',
  production: 'orchestration',
  hybrid: 'orchestration',
  'automatic-discovery': 'orchestration',
  roadmap: 'orchestration',

  // → security
  'prompt injection defense': 'security',
  'inter-agent-security': 'security',
  'agent security': 'security',
  compliance: 'security',
  'formal-verification': 'security',
  'formal-safety': 'security',

  // → evaluation
  benchmark: 'evaluation',
  benchmarking: 'evaluation',
  survey: 'evaluation',
  'autonomous agent evaluation benchmarks': 'evaluation',
  'evaluation-methods': 'evaluation',
  'LLM evaluation': 'evaluation',
  'hallucination-detection': 'evaluation',
  'self-evaluation': 'evaluation',
  'confidence-probe': 'evaluation',
  'confidence-aware': 'evaluation',
  verifier: 'evaluation',
  observability: 'evaluation',
  theoretical: 'evaluation',
  'post-deployment': 'evaluation',
  capability: 'evaluation',
  'knowledge-gaps': 'evaluation',
  helm: 'evaluation',

  // → safety
  'agentic-safety': 'safety',
  'agentic AI safety': 'safety',
  'safety-evaluation': 'safety',
  'reward-modeling': 'safety',
  'constitutional-ai': 'safety',
  'hazard-analysis': 'safety',
  stpa: 'safety',
  'risk-assessment': 'safety',
  'fault-tolerance': 'safety',
  'ai-feedback': 'safety',
  rlaif: 'safety',
  'human-in-the-loop': 'safety',
  feedback: 'safety',
  'feedback-loop': 'safety',
  'feedback-learning': 'safety',
  anthropic: 'safety',

  // → planning
  'decision-making': 'planning',
  'tree-search': 'planning',
  'tree-structure': 'planning',
  'multi-tree': 'planning',
  mcts: 'planning',
  backtrack: 'planning',
  hindsight: 'planning',
  'task-specific': 'planning',

  // → tool-use
  'tool use': 'tool-use',
  'tool-augmented reasoning and function calling': 'tool-use',
  'tool routing optimization': 'tool-use',
  'dynamic-linking': 'tool-use',
  'Sparse outcome rewards reduce tool calls by 72%': 'tool-use',

  // → reasoning
  'test-time': 'reasoning',
  'test-time-scaling': 'reasoning',
  'inference-scaling': 'reasoning',
  'inference-optimization': 'reasoning',
  'self-critique': 'reasoning',
  'self-reflection': 'reasoning',
  'self-referential': 'reasoning',
  'self-play': 'reasoning',
  reflexion: 'reasoning',
  reflection: 'reasoning',
  'reasoning-optimization': 'reasoning',
  'meta-thinking': 'reasoning',
  scaling: 'reasoning',
  recursive: 'reasoning',
  'trajectory-abstraction': 'reasoning',
  'verbal-rl': 'reasoning',
  exploration: 'reasoning',
  'Reducible uncertainty modeling across agent trajectories': 'reasoning',
  'Environmental feedback reduces uncertainty at decision gates': 'reasoning',
  'reinforcement-learning': 'reasoning',
  transformer: 'reasoning',
  ssm: 'reasoning',
  'sparse-activation': 'reasoning',
  'behavior-distillation': 'reasoning',
  'training-free': 'reasoning',
  'transfer-learning': 'reasoning',
  'instruction-tuning': 'reasoning',
  'Dense process rewards improve performance by 22%': 'reasoning',
  'prompt-optimization': 'reasoning',
};

/**
 * Normalize a free-form topic string to a canonical topic.
 * Returns the canonical topic if a mapping exists, otherwise the original string.
 */
export function normalizeTopicToCanonical(topic: string): string {
  return TOPIC_ALIASES[topic] ?? topic;
}
