import { CODE_QUEUE_LIVENESS_CHECK_NAMES, runCodeQueueLivenessFixtureChecks } from "./src/code-queue-liveness-fixtures";

function optionValues(args: string[], name: string): string[] {
  const values: string[] = [];
  for (let index = 0; index < args.length; index += 1) {
    if (args[index] !== name) continue;
    const raw = args[index + 1];
    if (raw === undefined || raw.startsWith("--")) throw new Error(`${name} requires a check name`);
    values.push(...raw.split(",").map((item) => item.trim()).filter(Boolean));
    index += 1;
  }
  return values;
}

function main(): void {
  const only = optionValues(Bun.argv.slice(2), "--only");
  const unknown = only.filter((name) => !CODE_QUEUE_LIVENESS_CHECK_NAMES.includes(name as never));
  if (unknown.length > 0) throw new Error(`unknown Code Queue liveness check(s): ${unknown.join(", ")}`);
  const result = runCodeQueueLivenessFixtureChecks(only);
  process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
  if (!result.ok) process.exit(1);
}

if (import.meta.main) main();
