const SUPPORTED_EXTENSIONS = new Set([
  ".tsx",
  ".jsx",
  ".ts",
  ".js",
  ".scss",
  ".css",
  ".vue",
  ".svelte",
]);

export function normalizeScanSourcePath(path: string): string | null {
  const normalized = path.replace(/\\/g, "/").replace(/^\.\/+/, "");
  if (!normalized || normalized.startsWith("/") || normalized.includes("\0")) {
    return null;
  }

  const parts = normalized.split("/");
  if (parts.some((part) => part === "" || part === "." || part === "..")) {
    return null;
  }

  return parts.join("/");
}

export function isSupportedScanPath(path: string): boolean {
  const dot = path.lastIndexOf(".");
  if (dot < 0) return false;
  return SUPPORTED_EXTENSIONS.has(path.slice(dot).toLowerCase());
}

export function syntheticAbsPath(root: string, relPath: string): string {
  const normalizedRoot = root.replace(/\/+$/, "");
  return normalizedRoot ? `${normalizedRoot}/${relPath}` : relPath;
}
