import { basename } from "node:path";

export const SHELL_WRAPPERS: ReadonlySet<string> = new Set([
  "bash",
  "sh",
  "dash",
  "zsh",
  "fish",
  "env",
  "nice",
  "timeout",
  "command",
]);

export const TRANSPORT_SCRIPTS: ReadonlySet<string> = new Set([
  "remote-runner.sh",
  "runner.sh",
]);

export function isWrapperCommand(command: string): boolean {
  const base = basename(command.trim());
  return SHELL_WRAPPERS.has(base) || TRANSPORT_SCRIPTS.has(base);
}

// A wrapper cannot identify the program that ran: `bash script` covers every
// command inside script. Only argv that names the executable directly attests it.
export function directCommandIdentity(argv: string[]): string | null {
  const token = String(argv[0] ?? "").trim();
  if (!token || token.includes(" ")) return null;
  const base = basename(token);
  return !base || isWrapperCommand(base) ? null : base;
}
