/**
 * Compatibility shims that let Claude Code plugins install and run in
 * SANDCODE unmodified.
 *
 * Two surfaces:
 *   - `rewriteClaudePaths`: translates `~/.claude/…`, `$HOME/.claude/…`,
 *     `${HOME}/.claude/…`, `$CLAUDE_CONFIG_DIR`, and `~/.claude.json` into
 *     their SANDCODE equivalents. Applied inside plugin content so hook
 *     scripts and command bodies land on the right config directory.
 *   - `buildPluginSpawnEnv` + `PLUGIN_SPAWN_ENV_KEYS`: produce the env block
 *     for child processes SANDCODE spawns on behalf of a plugin (MCP, LSP).
 *     Seeds both SANDCODE_PLUGIN_ROOT / SANDCODE_PLUGIN_DATA and the legacy
 *     CLAUDE_PLUGIN_ROOT / CLAUDE_PLUGIN_DATA aliases, plus
 *     CLAUDE_CONFIG_DIR mirrored from SANDCODE_CONFIG_DIR if set.
 *
 * This file intentionally has zero project-internal imports so it can be
 * pulled in from tests without dragging the settings / commands graph.
 */

/**
 * Translate Claude config path literals into SANDCODE equivalents. The
 * matcher is deliberately narrow: only well-known roots (`~`, `$HOME`,
 * `${HOME}`, and the `$CLAUDE_CONFIG_DIR` env var) are rewritten so prose
 * and product names that happen to contain `claude` are left alone.
 */
export function rewriteClaudePaths(input: string): string {
  return (
    input
      // ~/.claude.json → ~/.sandcode.json (file form FIRST so it wins over dir)
      .replace(/(^|[^A-Za-z0-9_])~\/\.claude\.json\b/g, '$1~/.sandcode.json')
      .replace(/(^|[^A-Za-z0-9_])\$HOME\/\.claude\.json\b/g, '$1$HOME/.sandcode.json')
      .replace(/(^|[^A-Za-z0-9_])\$\{HOME\}\/\.claude\.json\b/g, '$1${HOME}/.sandcode.json')
      // ~/.claude/ → ~/.sandcode/
      .replace(/(^|[^A-Za-z0-9_])~\/\.claude\//g, '$1~/.sandcode/')
      .replace(/(^|[^A-Za-z0-9_])\$HOME\/\.claude\//g, '$1$HOME/.sandcode/')
      .replace(/(^|[^A-Za-z0-9_])\$\{HOME\}\/\.claude\//g, '$1${HOME}/.sandcode/')
      // $CLAUDE_CONFIG_DIR / ${CLAUDE_CONFIG_DIR}
      .replace(/\$CLAUDE_CONFIG_DIR\b/g, '$SANDCODE_CONFIG_DIR')
      .replace(/\$\{CLAUDE_CONFIG_DIR\}/g, '${SANDCODE_CONFIG_DIR}')
  )
}

/** Keys produced by buildPluginSpawnEnv — callers skip these when resolving. */
export const PLUGIN_SPAWN_ENV_KEYS: ReadonlySet<string> = new Set([
  'SANDCODE_PLUGIN_ROOT',
  'SANDCODE_PLUGIN_DATA',
  'CLAUDE_PLUGIN_ROOT',
  'CLAUDE_PLUGIN_DATA',
  'CLAUDE_CONFIG_DIR',
])

/**
 * Build the env block seeded into plugin child processes. `getPluginData`
 * is supplied by the caller so this module stays free of the plugin-
 * directory resolution chain (which pulls in settings / secure storage).
 */
export function buildPluginSpawnEnv(args: {
  pluginPath: string
  pluginData?: string
}): Record<string, string> {
  const env: Record<string, string> = {
    SANDCODE_PLUGIN_ROOT: args.pluginPath,
    CLAUDE_PLUGIN_ROOT: args.pluginPath,
  }
  if (args.pluginData) {
    env.SANDCODE_PLUGIN_DATA = args.pluginData
    env.CLAUDE_PLUGIN_DATA = args.pluginData
  }
  const configDir = process.env.SANDCODE_CONFIG_DIR
  if (configDir) {
    env.CLAUDE_CONFIG_DIR = configDir
  }
  return env
}
