import fs from "node:fs";

const CHAPTERS = JSON.parse(fs.readFileSync("./data.json", "utf-8"));

const CUTS = fs
  .readFileSync("../_sprites/cut.txt", "utf-8")
  .split("Вырезня:")
  .at(-1)
  ?.split("\n");

CUTS?.shift();
if (CUTS?.at(-1) == "") CUTS?.pop();

console.log(CUTS);

for (const chapter of Object.keys(CHAPTERS)) {
  const OriginalPath = `../_sprites/original/${chapter}`;
  const UnreadyPath = `../_sprites/unready`;

  if (!fs.existsSync(UnreadyPath)) fs.mkdirSync(UnreadyPath);

  fs.cpSync(OriginalPath, UnreadyPath, { recursive: true, force: true });
}

const TRANSLATED = fs.readdirSync("../_sprites/translation");
const UNREADY = fs.readdirSync("../_sprites/unready");

for (const sprite of TRANSLATED) {
  if (sprite.endsWith(".png")) {
    if (UNREADY.includes(sprite)) fs.rmSync(`../_sprites/unready/${sprite}`);
  } else {
    if (
      fs.existsSync(`../_sprites/unready/${sprite}`) &&
      fs.readdirSync(`../_sprites/unready/${sprite}`).length ==
        fs.readdirSync(`../_sprites/translation/${sprite}`).length
    )
      fs.rmSync(`../_sprites/unready/${sprite}`, {
        recursive: true,
        force: true,
      });
  }
}

if (CUTS != undefined) {
  for (const cut of CUTS) {
    if (fs.existsSync(`../_sprites/unready/${cut}`))
      fs.rmSync(`../_sprites/unready/${cut}`, {
        recursive: true,
        force: true,
      });
    else if (fs.existsSync(`../_sprites/unready/${cut}.png`))
      fs.rmSync(`../_sprites/unready/${cut}.png`, {
        recursive: true,
        force: true,
      });
  }
}
