import {bold, blue, dim, red, grey, underline, yellow} from "kleur/colors";
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 !== null) {
      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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvbG9nZ2VyLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBRUE7QUFDQTtBQUNBO0FBQ0E7QUFNQSwyQkFBbUM7QUFDakMsUUFBTSxnQkFBZ0I7QUFDdEIsTUFBSSxRQUFRLElBQUksTUFBTTtBQUNwQixVQUFNLGtCQUFrQixRQUFRLElBQUksS0FBSyxNQUFNLEtBQUssR0FBRyxRQUFRLE1BQU07QUFHckUsUUFBSSxnQkFBZ0IsU0FBUztBQUFHLGFBQ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