import {bold, blue, dim, red, grey, underline, yellow} from "kleur/colors";
import {performance} from "perf_hooks";
import {Writable} from "stream";
import stringWidth from "string-width";
import {format as utilFormat} from "util";
function getLoggerLocale() {
  const defaultLocale = "en-US";
  if (process.env.LANG) {
    const extractedLocale = process.env.LANG.split(".")[0].replace(/_/g, "-");
    if (extractedLocale.length < 2)
      return defaultLocale;
    else
      return extractedLocale;
  } else
    return defaultLocale;
}
const dt = new Intl.DateTimeFormat(getLoggerLocale(), {
  hour: "2-digit",
  minute: "2-digit"
});
const defaultLogDestination = new Writable({
  objectMode: true,
  write(event, _, callback) {
    let dest = process.stderr;
    if (levels[event.level] < levels["error"]) {
      dest = process.stdout;
    }
    dest.write(dim(dt.format(new Date()) + " "));
    let type = event.type;
    if (type) {
      if (event.level === "info") {
        type = bold(blue(type));
      } else if (event.level === "warn") {
        type = bold(yellow(type));
      } else if (event.level === "error") {
        type = bold(red(type));
      }
      dest.write(`[${type}] `);
    }
    dest.write(utilFormat(...event.args));
    dest.write("\n");
    callback();
  }
});
const defaultLogOptions = {
  dest: defaultLogDestination,
  level: "info"
};
const levels = {
  debug: 20,
  info: 30,
  warn: 40,
  error: 50,
  silent: 90
};
function log(opts = {}, level, type, ...args) {
  var _a, _b;
  const logLevel = (_a = opts.level) != null ? _a : defaultLogOptions.level;
  const dest = (_b = opts.dest) != null ? _b : defaultLogOptions.dest;
  const event = {
    type,
    level,
    args,
    message: ""
  };
  if (levels[logLevel] > levels[level]) {
    return;
  }
  dest.write(event);
}
function debug(opts, type, ...messages) {
  return log(opts, "debug", type, ...messages);
}
function info(opts, type, ...messages) {
  return log(opts, "info", type, ...messages);
}
function warn(opts, type, ...messages) {
  return log(opts, "warn", type, ...messages);
}
function error(opts, type, ...messages) {
  return log(opts, "error", type, ...messages);
}
function table(opts, columns) {
  return function logTable(logFn, ...input) {
    const messages = columns.map((len, i) => padStr(input[i].toString(), len));
    logFn(opts, null, ...messages);
  };
}
function parseError(opts, err) {
  if (!err.frame) {
    return error(opts, "parse-error", err.message || err);
  }
  let frame = err.frame.replace(/^([0-9]+)(:)/gm, `${bold("$1")} \u2502`).replace(/(?<=^\s+)(\^)/gm, bold(red(" ^"))).replace(/^/gm, "   ");
  error(opts, "parse-error", `
 ${underline(bold(grey(`${err.filename || ""}:${err.start.line}:${err.start.column}`)))}
 ${bold(red(`\u{1D605} ${err.message}`))}
${frame}
`);
}
const logger = {
  debug: debug.bind(null, defaultLogOptions),
  info: info.bind(null, defaultLogOptions),
  warn: warn.bind(null, defaultLogOptions),
  error: error.bind(null, defaultLogOptions)
};
function padStr(str, len) {
  const strLen = stringWidth(str);
  if (strLen > len) {
    return str.substring(0, len - 3) + "...";
  }
  const spaces = Array.from({length: len - strLen}, () => " ").join("");
  return str + spaces;
}
let defaultLogLevel;
if (process.argv.includes("--verbose")) {
  defaultLogLevel = "debug";
} else if (process.argv.includes("--silent")) {
  defaultLogLevel = "silent";
} else {
  defaultLogLevel = "info";
}
function timerMessage(message, startTime = performance.now()) {
  let timeDiff = performance.now() - startTime;
  let timeDisplay = timeDiff < 750 ? `${Math.round(timeDiff)}ms` : `${(timeDiff / 1e3).toFixed(1)}s`;
  return `${message}   ${dim(timeDisplay)}`;
}
export {
  debug,
  defaultLogDestination,
  defaultLogLevel,
  defaultLogOptions,
  error,
  info,
  levels,
  log,
  logger,
  parseError,
  table,
  timerMessage,
  warn
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvbG9nZ2VyLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBRUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQU1BLDJCQUFtQztBQUNqQyxRQUFNLGdCQUFnQjtBQUN0QixNQUFJLFFBQVEsSUFBSSxNQUFNO0FBQ3BCLFVBQU0sa0JBQWtCLFFBQVEsSUFBSSxLQUFLLE1BQU0sS0FBSyxHQUFHLFFBQVEsTUFBTTtBQUdyRSxRQUFJLG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