import { existsSync } from "fs";

import { asyncLoadJSON, asyncSaveJSON } from "../lib";

interface JSONCard {
  mcmId: number;
  set: string;
}

interface JSONSet {
  cards: JSONCard[];
}

type JSONDB = Record<string, JSONSet>;

async function run() {
  if (!existsSync("AllPrintings.json")) {
    console.error(
      "AllPrintings.json not found. Download from: https://www.mtgjson.com/files/AllPrintings.json"
    );
    process.exit(1);
  }
  console.info("Loading AllPrintings.json");
  const db = await asyncLoadJSON<JSONDB>("AllPrintings.json");
  let acc = {};
  for (const set in db) {
    db[set].cards.forEach(c => {
      c.set = set;
      acc[c.mcmId] = c;
    });
  }
  asyncSaveJSON("mcmCards.json", acc);
  console.info("Saved converted info to mcmCards.json. You're ready to go!");
}

run();
