// Builds a git repo whose working tree has one TS file changed by ~176 additions / ~126 deletions.
import { execFileSync } from "node:child_process";
import { mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";

const root = process.argv[2];
mkdirSync(root, { recursive: true });
const git = (...a) => execFileSync("git", a, { cwd: root, stdio: "pipe" });
git("init", "-q", "-b", "main");
git("config", "user.email", "t@example.com");
git("config", "user.name", "t");

function block(i, variant) {
  const v = variant ? "Next" : "";
  return [
    `export interface Record${i}${v} {`,
    `  readonly id: string;`,
    `  readonly count${i}: number;`,
    `  readonly label: "alpha" | "beta" | ${variant ? '"gamma" | "delta"' : '"gamma"'};`,
    `}`,
    ``,
    `export function compute${i}${v}(input: Record${i}${v}[], factor = ${i}): number {`,
    `  let total = 0;`,
    `  for (const item of input) {`,
    `    if (item.label === "alpha") total += item.count${i} * factor;`,
    variant
      ? `    else if (item.label === "delta") total -= Math.round(item.count${i} / (factor + 1));`
      : `    else total += item.count${i};`,
    `  }`,
    `  return ${variant ? `Math.max(0, total)` : "total"}; // block ${i}`,
    `}`,
    ``,
  ];
}

const N = 40;
const before = [];
const after = [];
for (let i = 0; i < N; i++) {
  before.push(...block(i, false));
  // Change ~every block: deletions from old, additions from new, plus some extra new lines.
  if (i % 4 === 3) after.push(...block(i, false));
  else {
    after.push(...block(i, true));
    if (i % 3 === 0) after.push(`export const extra${i} = compute${i}Next([], ${i}) + ${i};`, ``);
  }
}
writeFileSync(join(root, "big.ts"), before.join("\n") + "\n");
for (let f = 0; f < 7; f++) writeFileSync(join(root, `small${f}.ts`), `export const v${f} = ${f};\n`);
git("add", ".");
git("commit", "-q", "-m", "base");
writeFileSync(join(root, "big.ts"), after.join("\n") + "\n");
for (let f = 0; f < 7; f++) writeFileSync(join(root, `small${f}.ts`), `export const v${f} = ${f + 1};\n`);
console.log(execFileSync("git", ["diff", "--numstat"], { cwd: root }).toString());
