const TypeScriptAdapter = require('./typescript');
const PythonAdapter     = require('./python');
const GoAdapter         = require('./go');
const RustAdapter       = require('./rust');
const RubyAdapter       = require('./ruby');

/**
 * LSP adapter registry.
 *
 * Adding a new language (Java, C/C++, PHP, Elixir, …):
 *   1. Create `src/utils/lsp-adapters/<lang>.js` exporting a class with the
 *      same shape as TypeScriptAdapter (name, label, binary, installMode,
 *      installCommand, verifyCommand, claudePluginId, static detect).
 *   2. Add it to the REGISTRY below.
 *   3. (Optional) Wire a Claude Code plugin id if one exists.
 *
 * Adapters describe *how* to install/verify a language server; they do NOT
 * speak LSP themselves. The actual symbol search at runtime happens through
 * whatever Claude Code plugin or MCP server the user has loaded.
 */
const REGISTRY = {
  typescript: TypeScriptAdapter,
  python:     PythonAdapter,
  go:         GoAdapter,
  rust:       RustAdapter,
  ruby:       RubyAdapter
};

function listAdapters() { return Object.keys(REGISTRY); }

function getAdapter(name, cwd) {
  const Cls = REGISTRY[name];
  if (!Cls) throw new Error(`Unknown LSP adapter: ${name}. Available: ${listAdapters().join(', ')}`);
  return new Cls(cwd);
}

/**
 * Probe the cwd for every known language and return the names of adapters
 * whose static detect() fires. Pure: no install side effects.
 */
function detectAll(cwd = process.cwd()) {
  return Object.entries(REGISTRY)
    .filter(([, Cls]) => {
      try { return Cls.detect(cwd); } catch { return false; }
    })
    .map(([name]) => name);
}

module.exports = { REGISTRY, listAdapters, getAdapter, detectAll };
