import { consoleLogSink, TeeLogSink } from "@rocicorp/logger";
import { DatadogLogSink } from "../../../datadog/src/datadog-log-sink.js";
import { appendPath } from "./http-string.js";
import { version } from "./version.js";
class LevelFilterLogSink {
  #wrappedLogSink;
  #level;
  constructor(wrappedLogSink, level) {
    this.#wrappedLogSink = wrappedLogSink;
    this.#level = level;
  }
  log(level, context, ...args) {
    if (this.#level === "error" && level !== "error") {
      return;
    }
    if (this.#level === "info" && level === "debug") {
      return;
    }
    this.#wrappedLogSink.log(level, context, ...args);
  }
  async flush() {
    await consoleLogSink.flush?.();
  }
}
const DATADOG_LOG_LEVEL = "info";
const ZERO_SASS_DOMAIN = ".reflect-server.net";
function createLogOptions(options, createDatadogLogSink = (options2) => new DatadogLogSink(options2)) {
  const { consoleLogLevel, server, enableAnalytics } = options;
  if (!enableAnalytics || server === null) {
    return {
      logLevel: consoleLogLevel,
      logSink: consoleLogSink
    };
  }
  const serverURL = new URL(server);
  const { hostname } = serverURL;
  const datadogServiceLabel = hostname.endsWith(ZERO_SASS_DOMAIN) ? hostname.substring(0, hostname.length - ZERO_SASS_DOMAIN.length).toLowerCase() : hostname;
  const baseURL = new URL(appendPath(server, "/logs/v0/log"));
  const logLevel = consoleLogLevel === "debug" ? "debug" : "info";
  const logSink = new TeeLogSink([
    new LevelFilterLogSink(consoleLogSink, consoleLogLevel),
    new LevelFilterLogSink(
      createDatadogLogSink({
        service: datadogServiceLabel,
        host: location.host,
        version,
        baseURL
      }),
      DATADOG_LOG_LEVEL
    )
  ]);
  return {
    logLevel,
    logSink
  };
}
export {
  createLogOptions
};
//# sourceMappingURL=log-options.js.map
