import { readFile, writeFile } from "fs/promises";
import { join } from "path";
import generated from "../artifacts/docs.json";
import info from "./info.json";
import queryTemplate from "./queries";
import txTemplate from "./transactions";

(async () => {
  const path = join(process.cwd(), "docs");

  const content =
    (await readFile(join(path, "header.md"), { encoding: "utf8" })) +
    "\n" +
    (await readFile(join(path, "setup.md"), { encoding: "utf8" })) +
    "\n" +
    (await readFile(join(path, "usage.md"), { encoding: "utf8" })) +
    "\n" +
    (await readFile(join(path, "types.md"), { encoding: "utf8" })) +
    "\n" +
    (await readFile(join(path, "methods.md"), { encoding: "utf8" })) +
    "\n" +
    (await readFile(join(path, "queries.md"), { encoding: "utf8" })) +
    "\n" +
    generated.queries
      .map((details) =>
        queryTemplate(
          details as any,
          // @ts-ignore
          info.testnet.queries.includes(details.method),
        ),
      )
      .join("  \n\n") +
    "\n" +
    (await readFile(join(path, "transactions.md"), { encoding: "utf8" })) +
    "\n" +
    generated.txs
      .map((details) =>
        // @ts-ignore
        txTemplate(details as any, info.testnet.txs.includes(details.method)),
      )
      .join("  \n\n") +
    "\n";

  await writeFile(join(path, "README.md"), content, { encoding: "utf8" });
})().catch((err) => process.stderr.write(`${err.message}\n`));
