import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-DyL154ka.js";
import { o as getTailnetHostname } from "./tailscale-BrlTrLr8.js";
import { g as parseCanonicalIpAddress, i as isCanonicalDottedDecimalIPv4, l as isIpv6Address } from "./ip-WAe3-2zT.js";
//#region src/gateway/gateway-config-prompts.shared.ts
const TAILSCALE_EXPOSURE_OPTIONS = [
	{
		value: "off",
		label: "Off",
		hint: "无 Tailscale 暴露"
	},
	{
		value: "serve",
		label: "Serve",
		hint: "Tailnet 私有 HTTPS (Tailscale 设备)"
	},
	{
		value: "funnel",
		label: "Funnel",
		hint: "通过 Tailscale Funnel 的公共 HTTPS (互联网)"
	}
];
const TAILSCALE_MISSING_BIN_NOTE_LINES = [
	"在 PATH 或 /Applications 中未找到 Tailscale 二进制文件。",
	"请确保已安装 Tailscale：",
	"  https://tailscale.com/download/mac",
	"",
	"您可以继续设置，但 serve/funnel 将在运行时失败。"
];
const TAILSCALE_DOCS_LINES = [
	"Docs:",
	"https://docs.openclaw.ai/gateway/tailscale",
	"https://docs.openclaw.ai/web"
];
function normalizeTailnetHostForUrl(rawHost) {
	const trimmed = rawHost.trim().replace(/\.$/, "");
	if (!trimmed) return null;
	const parsed = parseCanonicalIpAddress(trimmed);
	if (parsed && isIpv6Address(parsed)) return `[${normalizeLowercaseStringOrEmpty(parsed.toString())}]`;
	return trimmed;
}
function buildTailnetHttpsOrigin(rawHost) {
	const normalizedHost = normalizeTailnetHostForUrl(rawHost);
	if (!normalizedHost) return null;
	try {
		return new URL(`https://${normalizedHost}`).origin;
	} catch {
		return null;
	}
}
function appendAllowedOrigin(existing, origin) {
	const current = existing ?? [];
	const normalized = normalizeLowercaseStringOrEmpty(origin);
	if (current.some((entry) => normalizeLowercaseStringOrEmpty(entry) === normalized)) return current;
	return [...current, origin];
}
async function maybeAddTailnetOriginToControlUiAllowedOrigins(params) {
	if (params.tailscaleMode !== "serve" && params.tailscaleMode !== "funnel") return params.config;
	const tsOrigin = await getTailnetHostname(void 0, params.tailscaleBin ?? void 0).then((host) => buildTailnetHttpsOrigin(host)).catch(() => null);
	if (!tsOrigin) return params.config;
	const updatedOrigins = appendAllowedOrigin(params.config.gateway?.controlUi?.allowedOrigins ?? [], tsOrigin);
	return {
		...params.config,
		gateway: {
			...params.config.gateway,
			controlUi: {
				...params.config.gateway?.controlUi,
				allowedOrigins: updatedOrigins
			}
		}
	};
}
//#endregion
//#region src/shared/net/ipv4.ts
function validateDottedDecimalIPv4Input(value) {
	if (!value) return "自定义绑定模式需要 IP 地址";
	if (isCanonicalDottedDecimalIPv4(value)) return;
	return "无效的 IPv4 地址 (例如 192.168.1.100)";
}
/** @deprecated Use validateDottedDecimalIPv4Input. */
function validateIPv4AddressInput(value) {
	return validateDottedDecimalIPv4Input(value);
}
//#endregion
export { maybeAddTailnetOriginToControlUiAllowedOrigins as a, TAILSCALE_MISSING_BIN_NOTE_LINES as i, TAILSCALE_DOCS_LINES as n, TAILSCALE_EXPOSURE_OPTIONS as r, validateIPv4AddressInput as t };
