'use strict';

// Provider registry for multi-CLI auto-approval.
// Detection order: most distinctive signals first to minimize false positives.

const claudeCode = require('./claude-code');
const codexMcp = require('./codex-mcp');
const codex = require('./codex');
const gemini = require('./gemini');
const amazonQ = require('./amazon-q');
const aider = require('./aider');
const cursor = require('./cursor');
const cline = require('./cline');
const copilot = require('./copilot');
const kimi = require('./kimi');
const opencode = require('./opencode');

// Ordered by detection specificity — strong unique signals first,
// weak inline y/n prompts last to avoid false positives.
// Numbered-widget providers (Claude Code, Codex, Cursor, OpenCode, Kimi)
// run before inline [Y/n] providers (Gemini, Aider, Amazon Q, Cline, Copilot).
const providers = [
  codexMcp,
  claudeCode,
  codex,
  cursor,
  opencode,
  kimi,
  gemini,
  amazonQ,
  aider,
  cline,
  copilot,
];

const providerMap = new Map(providers.map(p => [p.id, p]));

// Detect which provider's approval prompt is on screen.
// Returns the provider object or null if no provider matches.
function detectProvider(text) {
  for (const provider of providers) {
    if (provider.detect(text)) return provider;
  }
  return null;
}

// Get a specific provider by ID.
function getProvider(id) {
  return providerMap.get(id) || null;
}

// Detect approval and return both the detection result and provider ID.
// Used by headless-term-worker as a drop-in enhancement over detectApproval().
function detectApprovalWithProvider(text) {
  const provider = detectProvider(text);
  if (provider) {
    return { detected: true, providerId: provider.id };
  }
  return { detected: false, providerId: null };
}

module.exports = {
  providers,
  detectProvider,
  getProvider,
  detectApprovalWithProvider,
};
