import type { ScanSource } from "./types.js";
import { normalizeScanSourcePath } from "./paths.js";

export function createMemoryScanSource(
  files: Record<string, string>,
  options: { root?: string; kind?: "memory" | "fixture"; ref?: string } = {}
): ScanSource {
  const normalizedFiles = new Map<string, string>();
  for (const [path, content] of Object.entries(files)) {
    const relPath = normalizeScanSourcePath(path);
    if (relPath) normalizedFiles.set(relPath, content);
  }

  return {
    kind: options.kind ?? "memory",
    root: options.root ?? "memory://repo",
    ...(options.ref ? { ref: options.ref } : {}),
    async readFile(path: string): Promise<string | null> {
      const relPath = normalizeScanSourcePath(path);
      if (!relPath) return null;
      return normalizedFiles.get(relPath) ?? null;
    },
  };
}
