import fs from "node:fs/promises";
import path from "node:path";

const __dirname = process.cwd();

interface Cache {
  certificates:
    | {
        signerCert: Buffer | string;
        signerKey: Buffer | string;
        wwdr: Buffer | string;
        signerKeyPassphrase: string;
      }
    | undefined;
}

const cache: Cache = {
  certificates: undefined,
};

export async function getCertificates(): Promise<
  Exclude<Cache["certificates"], undefined>
> {
  if (cache.certificates) {
    return cache.certificates;
  }

  const [signerCert, signerKey, wwdr, signerKeyPassphrase] = await Promise.all([
    fs.readFile(path.resolve(__dirname, "certs/signerCert.pem"), "utf-8"),
    fs.readFile(path.resolve(__dirname, "certs/signerKey.pem"), "utf-8"),
    fs.readFile(path.resolve(__dirname, "certs/WWDR.pem"), "utf-8"),
    fs.readFile(path.resolve(__dirname, "certs/passPhrase.txt"), "utf-8"),
  ]);

  cache.certificates = {
    signerCert,
    signerKey,
    wwdr,
    signerKeyPassphrase,
  };

  return cache.certificates;
}
