import { loadTunnelState } from "../tunnel/tunnel-state.js";
import { resolveLanGatewayUrl } from "../tunnel/tunnel-qr.js";
//#region src/share/share-url.ts
function resolveShareUrl(token, ctx) {
	const tunnelState = loadTunnelState();
	const { gatewayHost, gatewayPort, reverseProxyPublicUrl } = ctx;
	const path = `/s/${token}`;
	if (tunnelState?.publicUrl) return {
		shareUrl: `${tunnelState.publicUrl.replace(/\/+$/, "")}${path}`,
		lanUrl: buildLanUrl(gatewayHost, gatewayPort, path),
		reachability: "public",
		reachabilityHint: null
	};
	const reverseProxy = reverseProxyPublicUrl?.trim();
	if (reverseProxy) return {
		shareUrl: `${reverseProxy.replace(/\/+$/, "")}${path}`,
		lanUrl: buildLanUrl(gatewayHost, gatewayPort, path),
		reachability: "public",
		reachabilityHint: null
	};
	if (!isLoopbackHost(gatewayHost)) return {
		shareUrl: `http://${gatewayHost}:${gatewayPort}${path}`,
		lanUrl: null,
		reachability: "lan",
		reachabilityHint: "当前局域网内可访问，开启远程隧道后对外网可达"
	};
	return {
		shareUrl: `http://localhost:${gatewayPort}${path}`,
		lanUrl: null,
		reachability: "local-only",
		reachabilityHint: "当前仅本机可访问，开启远程隧道后对外可达"
	};
}
function buildLanUrl(host, port, path) {
	const lanBase = resolveLanGatewayUrl(host, port);
	if (!lanBase) return null;
	return `${lanBase.replace(/\/+$/, "")}${path}`;
}
function isLoopbackHost(host) {
	return host === "127.0.0.1" || host === "localhost" || host === "::1";
}
function resolveReachabilityForList(ctx) {
	if (loadTunnelState()?.publicUrl) return "public";
	if (ctx.reverseProxyPublicUrl?.trim()) return "public";
	if (!isLoopbackHost(ctx.gatewayHost)) return "lan";
	return "local-only";
}
/**
* Resolve the URL pair for a site share. Priority mirrors `resolveShareUrl`:
*  1. FRP tunnel → wildcard subdomain on `<publicHostSuffix>`.
*  2. Reverse-proxy `publicUrl` → `<publicUrl>/site/<token>/` subpath.
*  3. Direct gateway bind → `http://<host>:<port>/site/<token>/`.
*/
function resolveSiteShareUrl(ctx) {
	if (loadTunnelState()?.publicUrl) {
		const root = `https://${ctx.subdomainLabel}.${ctx.publicHostSuffix}`;
		return {
			shareUrl: `${root}/`,
			thumbnailUrl: `${root}/site/${ctx.token}/thumbnail`,
			reachability: "public",
			reachabilityHint: null
		};
	}
	const reverseProxy = ctx.reverseProxyPublicUrl?.trim();
	if (reverseProxy) {
		const base = reverseProxy.replace(/\/+$/, "");
		return {
			shareUrl: `${base}/site/${ctx.token}/`,
			thumbnailUrl: `${base}/site/${ctx.token}/thumbnail`,
			reachability: "public",
			reachabilityHint: null
		};
	}
	const base = `http://${ctx.gatewayHost}:${ctx.gatewayPort}`;
	const loopback = isLoopbackHost(ctx.gatewayHost);
	return {
		shareUrl: `${base}/site/${ctx.token}/`,
		thumbnailUrl: `${base}/site/${ctx.token}/thumbnail`,
		reachability: loopback ? "local-only" : "lan",
		reachabilityHint: loopback ? "当前仅本机可访问，开启远程隧道后对外可达" : "当前局域网内可访问，开启远程隧道后对外网可达"
	};
}
//#endregion
export { resolveReachabilityForList, resolveShareUrl, resolveSiteShareUrl };

//# sourceMappingURL=share-url.js.map