import { v as resolveStateDir } from "./paths-ZwPDM6zn.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 DAOCORE_DEBUG_PROXY_ENABLED = "DAOCORE_DEBUG_PROXY_ENABLED";
const DAOCORE_DEBUG_PROXY_URL = "DAOCORE_DEBUG_PROXY_URL";
const DAOCORE_DEBUG_PROXY_DB_PATH = "DAOCORE_DEBUG_PROXY_DB_PATH";
const DAOCORE_DEBUG_PROXY_BLOB_DIR = "DAOCORE_DEBUG_PROXY_BLOB_DIR";
const DAOCORE_DEBUG_PROXY_CERT_DIR = "DAOCORE_DEBUG_PROXY_CERT_DIR";
const DAOCORE_DEBUG_PROXY_SESSION_ID = "DAOCORE_DEBUG_PROXY_SESSION_ID";
const DAOCORE_DEBUG_PROXY_REQUIRE = "DAOCORE_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[DAOCORE_DEBUG_PROXY_ENABLED]);
	const sessionId = (env["DAOCORE_DEBUG_PROXY_SESSION_ID"]?.trim() || void 0) ?? (cachedImplicitSessionId ??= randomUUID());
	return {
		enabled,
		required: isTruthy(env[DAOCORE_DEBUG_PROXY_REQUIRE]),
		proxyUrl: env["DAOCORE_DEBUG_PROXY_URL"]?.trim() || void 0,
		dbPath: env["DAOCORE_DEBUG_PROXY_DB_PATH"]?.trim() || resolveDebugProxyDbPath(env),
		blobDir: env["DAOCORE_DEBUG_PROXY_BLOB_DIR"]?.trim() || resolveDebugProxyBlobDir(env),
		certDir: env["DAOCORE_DEBUG_PROXY_CERT_DIR"]?.trim() || resolveDebugProxyCertDir(env),
		sessionId,
		sourceProcess: "daocore"
	};
}
function applyDebugProxyEnv(env, params) {
	return {
		...env,
		[DAOCORE_DEBUG_PROXY_ENABLED]: "1",
		[DAOCORE_DEBUG_PROXY_REQUIRE]: "1",
		[DAOCORE_DEBUG_PROXY_URL]: params.proxyUrl,
		[DAOCORE_DEBUG_PROXY_DB_PATH]: params.dbPath ?? resolveDebugProxyDbPath(env),
		[DAOCORE_DEBUG_PROXY_BLOB_DIR]: params.blobDir ?? resolveDebugProxyBlobDir(env),
		[DAOCORE_DEBUG_PROXY_CERT_DIR]: params.certDir ?? resolveDebugProxyCertDir(env),
		[DAOCORE_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 };
