import { y as resolveStateDir } from "./paths-CQLGtQnw.js";
import process$1 from "node:process";
import path from "node:path";
import { randomUUID } from "node:crypto";
import { createAmbientNodeProxyAgent } from "@openclaw/proxyline";
//#region src/proxy-capture/paths.ts
function resolveDebugProxyRootDir(env = process.env) {
	return path.join(resolveStateDir(env), "debug-proxy");
}
function resolveDebugProxyDbPath(env = process.env) {
	return path.join(resolveDebugProxyRootDir(env), "capture.sqlite");
}
function resolveDebugProxyBlobDir(env = process.env) {
	return path.join(resolveDebugProxyRootDir(env), "blobs");
}
function resolveDebugProxyCertDir(env = process.env) {
	return path.join(resolveDebugProxyRootDir(env), "certs");
}
//#endregion
//#region src/proxy-capture/env.ts
const DEX_DEBUG_PROXY_ENABLED = "DEX_DEBUG_PROXY_ENABLED";
const DEX_DEBUG_PROXY_URL = "DEX_DEBUG_PROXY_URL";
const DEX_DEBUG_PROXY_DB_PATH = "DEX_DEBUG_PROXY_DB_PATH";
const DEX_DEBUG_PROXY_BLOB_DIR = "DEX_DEBUG_PROXY_BLOB_DIR";
const DEX_DEBUG_PROXY_CERT_DIR = "DEX_DEBUG_PROXY_CERT_DIR";
const DEX_DEBUG_PROXY_SESSION_ID = "DEX_DEBUG_PROXY_SESSION_ID";
const DEX_DEBUG_PROXY_REQUIRE = "DEX_DEBUG_PROXY_REQUIRE";
let cachedImplicitSessionId;
function isTruthy(value) {
	return value === "1" || value === "true" || value === "yes" || value === "on";
}
function resolveDebugProxySettings(env = process$1.env) {
	const enabled = isTruthy(env[DEX_DEBUG_PROXY_ENABLED]);
	const sessionId = (env["DEX_DEBUG_PROXY_SESSION_ID"]?.trim() || void 0) ?? (cachedImplicitSessionId ??= randomUUID());
	return {
		enabled,
		required: isTruthy(env[DEX_DEBUG_PROXY_REQUIRE]),
		proxyUrl: env["DEX_DEBUG_PROXY_URL"]?.trim() || void 0,
		dbPath: env["DEX_DEBUG_PROXY_DB_PATH"]?.trim() || resolveDebugProxyDbPath(env),
		blobDir: env["DEX_DEBUG_PROXY_BLOB_DIR"]?.trim() || resolveDebugProxyBlobDir(env),
		certDir: env["DEX_DEBUG_PROXY_CERT_DIR"]?.trim() || resolveDebugProxyCertDir(env),
		sessionId,
		sourceProcess: "openclaw"
	};
}
function applyDebugProxyEnv(env, params) {
	return {
		...env,
		[DEX_DEBUG_PROXY_ENABLED]: "1",
		[DEX_DEBUG_PROXY_REQUIRE]: "1",
		[DEX_DEBUG_PROXY_URL]: params.proxyUrl,
		[DEX_DEBUG_PROXY_DB_PATH]: params.dbPath ?? resolveDebugProxyDbPath(env),
		[DEX_DEBUG_PROXY_BLOB_DIR]: params.blobDir ?? resolveDebugProxyBlobDir(env),
		[DEX_DEBUG_PROXY_CERT_DIR]: params.certDir ?? resolveDebugProxyCertDir(env),
		[DEX_DEBUG_PROXY_SESSION_ID]: params.sessionId,
		HTTP_PROXY: params.proxyUrl,
		HTTPS_PROXY: params.proxyUrl,
		ALL_PROXY: params.proxyUrl
	};
}
function createDebugProxyWebSocketAgent(settings) {
	if (!settings.enabled || !settings.proxyUrl) return;
	return createAmbientNodeProxyAgent({
		protocol: "https",
		env: {
			HTTP_PROXY: settings.proxyUrl,
			HTTPS_PROXY: settings.proxyUrl,
			ALL_PROXY: void 0,
			NO_PROXY: void 0,
			http_proxy: void 0,
			https_proxy: void 0,
			all_proxy: void 0,
			no_proxy: void 0
		}
	});
}
function resolveEffectiveDebugProxyUrl(configuredProxyUrl) {
	const explicit = configuredProxyUrl?.trim();
	if (explicit) return explicit;
	const settings = resolveDebugProxySettings();
	return settings.enabled ? settings.proxyUrl : void 0;
}
//#endregion
export { resolveEffectiveDebugProxyUrl as i, createDebugProxyWebSocketAgent as n, resolveDebugProxySettings as r, applyDebugProxyEnv as t };
