import { ensureFile } from "$std/fs/ensure_file.ts";
import { Modrinth, ModrinthGameVersion } from "../lib/modrinth.ts";

export const getGameVersions = async (releaseOnly?: boolean) => {
  const cacheFile = "./cache/gameVersions.json";
  await ensureFile(cacheFile);

  let versions: ModrinthGameVersion[] = JSON.parse(
    await Deno.readTextFile(cacheFile) || "[]",
  );

  if (!versions.length) {
    versions = await Modrinth.getGameVersions();
    await Deno.writeTextFile(cacheFile, JSON.stringify(versions));
  }

  return versions.filter(v => !releaseOnly || v.major);
};
