import initCycleTLS from "cycletls";

let cycleTLS;

const port = process.env.CYCLETLS_PORT ? Number(process.env.CYCLETLS_PORT) : undefined;
initCycleTLS(port ? { port } : {}).then((c) => {
  cycleTLS = c;
});

export default async function getCycleTLS() {
  if (!cycleTLS) {
    await new Promise((r) => {
      const c = setInterval(() => {
        if (cycleTLS) {
          clearInterval(c);
          r();
        }
      }, 10);
    });
  }

  return cycleTLS;
}

process.on("exit", () => {
  if (cycleTLS) cycleTLS.exit();
});
