import "../daemon/constants.js";
//#region src/gateway/host.ts
/** True when the bind address is local-only (127.x, localhost, ::1). */
function isLoopbackHost(host) {
	if (!host) return true;
	const normalized = host.trim().toLowerCase();
	return normalized === "127.0.0.1" || normalized === "localhost" || normalized === "::1" || normalized === "0:0:0:0:0:0:0:1";
}
/** True when the gateway listens on all interfaces. */
function isAllInterfacesHost(host) {
	if (!host) return false;
	const normalized = host.trim();
	return normalized === "0.0.0.0" || normalized === "::" || normalized === "*";
}
/** Vite dev server origins for the gateway console (`web/` defaults to port 3000). */
const GATEWAY_DEV_CONSOLE_ORIGINS = ["http://localhost:3000", "http://127.0.0.1:3000"];
/** Effective HTTP listen port: CLI `--port` override wins over config (default 18790). */
function resolveEffectiveGatewayPort(config, listenPortOverride) {
	if (typeof listenPortOverride === "number" && Number.isFinite(listenPortOverride)) return listenPortOverride;
	return config.gateway?.port ?? 18790;
}
/** Resolve listen port from a gateway service (supports partial test mocks without `getEffectiveListenPort`). */
function resolveGatewayServiceListenPort(service) {
	if (typeof service.getEffectiveListenPort === "function") return service.getEffectiveListenPort();
	return resolveEffectiveGatewayPort(service.currentConfig);
}
function buildDefaultCorsOrigins(params) {
	const origins = new Set([
		`http://localhost:${params.port}`,
		`http://127.0.0.1:${params.port}`,
		...GATEWAY_DEV_CONSOLE_ORIGINS
	]);
	const bindHost = params.bindHost?.trim();
	if (bindHost && !isLoopbackHost(bindHost) && !isAllInterfacesHost(bindHost)) origins.add(`http://${bindHost}:${params.port}`);
	return [...origins];
}
/**
* Effective browser origins for CORS and CSRF checks.
* Custom `gateway.corsOrigins` (e.g. after LAN pairing) still merge loopback Vite dev origins.
*/
function resolveGatewayCorsOrigins(params) {
	const configured = (params.configuredOrigins ?? []).map((origin) => origin.trim()).filter(Boolean);
	if (configured.length === 0) return buildDefaultCorsOrigins({
		port: params.port,
		bindHost: params.bindHost
	});
	return [...new Set([...configured, ...GATEWAY_DEV_CONSOLE_ORIGINS])];
}
/** Browser origin (`https://host`) from a gateway public/tunnel root URL. */
function originFromGatewayPublicUrl(publicUrl) {
	const trimmed = publicUrl?.trim();
	if (!trimmed) return null;
	try {
		return new URL(trimmed).origin.toLowerCase();
	} catch {
		return null;
	}
}
/** CORS + CSRF allowlist including active FRP tunnel + reverse-proxy origins. */
function resolveAllowedBrowserOrigins(params) {
	const origins = resolveGatewayCorsOrigins({
		configuredOrigins: params.configuredOrigins,
		port: params.port,
		bindHost: params.bindHost
	});
	const tunnelOrigin = originFromGatewayPublicUrl(params.tunnelPublicUrl);
	if (tunnelOrigin && !origins.includes(tunnelOrigin)) origins.push(tunnelOrigin);
	const reverseProxyOrigin = originFromGatewayPublicUrl(params.reverseProxyPublicUrl);
	if (reverseProxyOrigin && !origins.includes(reverseProxyOrigin)) origins.push(reverseProxyOrigin);
	return origins;
}
//#endregion
export { GATEWAY_DEV_CONSOLE_ORIGINS, buildDefaultCorsOrigins, isAllInterfacesHost, isLoopbackHost, originFromGatewayPublicUrl, resolveAllowedBrowserOrigins, resolveEffectiveGatewayPort, resolveGatewayCorsOrigins, resolveGatewayServiceListenPort };

//# sourceMappingURL=host.js.map