import { nonexhaustive } from "@likec4/core";
import { formatLogObj, LogLevels, rootLogger as root } from "@likec4/log";
import { BROWSER } from "esm-env";
export const logger = root.withTag("lsp");
export function logError(err) {
  logger.error(err);
}
export function logWarnError(err) {
  logger.warn(err);
}
export function setLogLevel(level) {
  logger.level = LogLevels[level];
}
export function logToLspConnection(connection) {
  const reporter = {
    log: (logObj, _ctx) => {
      const { message, error } = formatLogObj(logObj);
      switch (logObj.type) {
        case "silent": {
          break;
        }
        case "verbose":
        case "trace": {
          connection.tracer.log(message);
          break;
        }
        case "debug": {
          connection.console.debug(message);
          break;
        }
        case "log": {
          connection.console.log(message);
          break;
        }
        case "info":
        case "box":
        case "ready":
        case "start":
        case "success": {
          connection.console.info(message);
          break;
        }
        case "warn": {
          connection.console.warn(message);
          break;
        }
        case "fail":
        case "error":
        case "fatal": {
          connection.console.error(message);
          if (error) {
            connection.telemetry.logEvent({ eventName: "error", ...error });
          } else {
            connection.telemetry.logEvent({ eventName: "error", message });
          }
          break;
        }
        default:
          nonexhaustive(logObj.type);
      }
    }
  };
  if (BROWSER) {
    root.addReporter(reporter);
  } else {
    root.setReporters([reporter]);
  }
  logger.setReporters(root.options.reporters);
}
