import { join } from "@std/path";

export async function getAsciiArt(art: string) {
  try {
    const artFilePath =
      Deno.env.get("BEARMETAL_ASCII_PATH") || import.meta.dirname
        ? join(import.meta.dirname || "", "../asciiart.txt")
        : "https://git.cyborggrizzly.com/BearMetal/pdf-tools/raw/branch/main/asciiart.txt";
    let artFileText: string;
    if (artFilePath?.startsWith("http")) {
      artFileText = await fetch(artFilePath).then((res) => res.text());
    } else {
      artFileText = await Deno.readTextFile(
        artFilePath,
      );
    }
    const parserRX = /begin\s+(\w+)\s*\n([\s\S]*?)\s*end\s*/g;
    let result = parserRX.exec(artFileText);

    while (result !== null) {
      const [_, name, artText] = result;
      if (name === art) return artText;
      result = parserRX.exec(artFileText);
    }
  } catch (e) {
    console.log(e);
    alert();
  }
  return art;
}
