// @bun
import {
  color,
  init_source,
  init_src,
  source_default,
  supportsHyperlinks
} from "./chunk-2g0ytd9d.js";
import {
  execFileNoThrow,
  init_execFileNoThrow
} from "./chunk-hzmzwte2.js";
import {
  getPreferredUserConfigFile,
  getUserConfigFiles,
  init_debug,
  init_errors,
  init_log,
  init_redscopeCompat,
  isENOENT,
  logError,
  logForDebugging,
  uniquePaths
} from "./chunk-s3hafnk3.js";
import {
  __esm
} from "./chunk-hhsxm2yr.js";

// src/utils/completionCache.ts
import { existsSync } from "fs";
import { mkdir, readFile, writeFile } from "fs/promises";
import { homedir } from "os";
import { dirname, join } from "path";
import { pathToFileURL } from "url";
function detectShell() {
  const shell = process.env.SHELL || "";
  const home = homedir();
  if (shell.endsWith("/zsh") || shell.endsWith("/zsh.exe")) {
    const cacheFile = getPreferredUserConfigFile("completion.zsh");
    return {
      name: "zsh",
      rcFile: join(home, ".zshrc"),
      cacheFile,
      cacheFiles: getUserConfigFiles("completion.zsh"),
      completionLine: `[[ -f "${cacheFile}" ]] && source "${cacheFile}"`,
      shellFlag: "zsh"
    };
  }
  if (shell.endsWith("/bash") || shell.endsWith("/bash.exe")) {
    const cacheFile = getPreferredUserConfigFile("completion.bash");
    return {
      name: "bash",
      rcFile: join(home, ".bashrc"),
      cacheFile,
      cacheFiles: getUserConfigFiles("completion.bash"),
      completionLine: `[ -f "${cacheFile}" ] && source "${cacheFile}"`,
      shellFlag: "bash"
    };
  }
  if (shell.endsWith("/fish") || shell.endsWith("/fish.exe")) {
    const xdg = process.env.XDG_CONFIG_HOME || join(home, ".config");
    const cacheFile = getPreferredUserConfigFile("completion.fish");
    return {
      name: "fish",
      rcFile: join(xdg, "fish", "config.fish"),
      cacheFile,
      cacheFiles: getUserConfigFiles("completion.fish"),
      completionLine: `[ -f "${cacheFile}" ] && source "${cacheFile}"`,
      shellFlag: "fish"
    };
  }
  return null;
}
function getCompletionCacheWriteFiles(shell) {
  return uniquePaths([
    shell.cacheFile,
    ...shell.cacheFiles.filter((file) => file !== shell.cacheFile && existsSync(file))
  ]);
}
function formatPathLink(filePath) {
  if (!supportsHyperlinks()) {
    return filePath;
  }
  const fileUrl = pathToFileURL(filePath).href;
  return `\x1B]8;;${fileUrl}\x07${filePath}\x1B]8;;\x07`;
}
async function setupShellCompletion(theme) {
  const shell = detectShell();
  if (!shell) {
    return "";
  }
  const redscopeBin = process.argv[1] || "redscope";
  for (const cacheFile of getCompletionCacheWriteFiles(shell)) {
    try {
      await mkdir(dirname(cacheFile), { recursive: true });
    } catch (e) {
      logError(e);
      return `${EOL}${color("warning", theme)(`Could not write ${shell.name} completion cache`)}${EOL}${source_default.dim(`Run manually: redscope completion ${shell.shellFlag} > ${cacheFile}`)}${EOL}`;
    }
    const result = await execFileNoThrow(redscopeBin, [
      "completion",
      shell.shellFlag,
      "--output",
      cacheFile
    ]);
    if (result.code !== 0) {
      return `${EOL}${color("warning", theme)(`Could not generate ${shell.name} shell completions`)}${EOL}${source_default.dim(`Run manually: redscope completion ${shell.shellFlag} > ${cacheFile}`)}${EOL}`;
    }
  }
  let existing = "";
  try {
    existing = await readFile(shell.rcFile, { encoding: "utf-8" });
    if (existing.includes("redscope completion") || existing.includes("claude completion") || shell.cacheFiles.some((cacheFile) => existing.includes(cacheFile))) {
      return `${EOL}${color("success", theme)(`Shell completions updated for ${shell.name}`)}${EOL}${source_default.dim(`See ${formatPathLink(shell.rcFile)}`)}${EOL}`;
    }
  } catch (e) {
    if (!isENOENT(e)) {
      logError(e);
      return `${EOL}${color("warning", theme)(`Could not install ${shell.name} shell completions`)}${EOL}${source_default.dim(`Add this to ${formatPathLink(shell.rcFile)}:`)}${EOL}${source_default.dim(shell.completionLine)}${EOL}`;
    }
  }
  try {
    const configDir = dirname(shell.rcFile);
    await mkdir(configDir, { recursive: true });
    const separator = existing && !existing.endsWith(`
`) ? `
` : "";
    const content = `${existing}${separator}
# RedScope AI shell completions
${shell.completionLine}
`;
    await writeFile(shell.rcFile, content, { encoding: "utf-8" });
    return `${EOL}${color("success", theme)(`Installed ${shell.name} shell completions`)}${EOL}${source_default.dim(`Added to ${formatPathLink(shell.rcFile)}`)}${EOL}${source_default.dim(`Run: source ${shell.rcFile}`)}${EOL}`;
  } catch (error) {
    logError(error);
    return `${EOL}${color("warning", theme)(`Could not install ${shell.name} shell completions`)}${EOL}${source_default.dim(`Add this to ${formatPathLink(shell.rcFile)}:`)}${EOL}${source_default.dim(shell.completionLine)}${EOL}`;
  }
}
async function regenerateCompletionCache() {
  const shell = detectShell();
  if (!shell) {
    return;
  }
  logForDebugging(`update: Regenerating ${shell.name} completion cache`);
  const claudeBin = process.argv[1] || "claude";
  for (const cacheFile of getCompletionCacheWriteFiles(shell)) {
    const result = await execFileNoThrow(claudeBin, [
      "completion",
      shell.shellFlag,
      "--output",
      cacheFile
    ]);
    if (result.code !== 0) {
      logForDebugging(`update: Failed to regenerate ${shell.name} completion cache at ${cacheFile}`);
      continue;
    }
    logForDebugging(`update: Regenerated ${shell.name} completion cache at ${cacheFile}`);
  }
}
var EOL = `
`;
var init_completionCache = __esm(() => {
  init_source();
  init_src();
  init_src();
  init_debug();
  init_errors();
  init_execFileNoThrow();
  init_log();
  init_redscopeCompat();
});

export { setupShellCompletion, regenerateCompletionCache, init_completionCache };
