import { appendPath } from "./http-string.js";
import { DatadogLogSink } from "../../../datadog/src/datadog-log-sink.js";
import { version } from "./version.js";
import { TeeLogSink, consoleLogSink } from "@rocicorp/logger";
//#region ../zero-client/src/client/log-options.ts
var LevelFilterLogSink = class {
	#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?.();
	}
};
var DATADOG_LOG_LEVEL = "info";
var ZERO_SASS_DOMAIN = ".reflect-server.net";
function createLogOptions(options, createDatadogLogSink = (options) => new DatadogLogSink(options)) {
	const { consoleLogLevel, server, enableAnalytics } = options;
	if (!enableAnalytics || server === null) return {
		logLevel: consoleLogLevel,
		logSink: consoleLogSink
	};
	const { hostname } = new URL(server);
	const datadogServiceLabel = hostname.endsWith(ZERO_SASS_DOMAIN) ? hostname.substring(0, hostname.length - 19).toLowerCase() : hostname;
	const baseURL = new URL(appendPath(server, "/logs/v0/log"));
	return {
		logLevel: consoleLogLevel === "debug" ? "debug" : "info",
		logSink: new TeeLogSink([new LevelFilterLogSink(consoleLogSink, consoleLogLevel), new LevelFilterLogSink(createDatadogLogSink({
			service: datadogServiceLabel,
			host: location.host,
			version,
			baseURL
		}), DATADOG_LOG_LEVEL)])
	};
}
//#endregion
export { createLogOptions };

//# sourceMappingURL=log-options.js.map