import { LogContext, logger } from "./logger.js";

export function sleep(seconds: number) {
  return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}

// TODO: Fix this the next time the file is edited.

export function timedRun(
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  promise: Promise<any>,
  seconds: number,
  message = "Promise timed out",
  logDetails = {},
  context: LogContext = "general",
  isWarn = false,
  throwOnTimeout = false,
) {
  // return Promise return value or log error if timeout is reached first
  // or throw if throwOnTimeout is true!
  const timeout = seconds * 1000;

  let tm: NodeJS.Timeout;

  const rejectPromiseOnTimeout = (timeout: number) => {
    return new Promise((resolve, reject) => {
      tm = setTimeout(() => reject("timeout reached"), timeout);
    });
  };

  return Promise.race([promise, rejectPromiseOnTimeout(timeout)])
    .catch((err) => {
      if (err === "timeout reached") {
        const logFunc = isWarn ? logger.warn : logger.error;
        logFunc.call(
          logger,
          message,
          { seconds: seconds, ...logDetails },
          context,
        );
        if (throwOnTimeout) {
          throw err;
        }
      } else {
        //logger.error("Unknown exception", {...errJSON(err), ...logDetails}, context);
        throw err;
      }
    })
    .finally(() => clearTimeout(tm));
}

export function secondsElapsed(startTime: number, nowDate: Date | null = null) {
  nowDate = nowDate || new Date();

  return (nowDate.getTime() - startTime) / 1000;
}

export function timestampNow() {
  return new Date().toISOString().replace(/[^\d]/g, "");
}
