import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { disableTailscaleFunnel, disableTailscaleServe, enableTailscaleFunnel, enableTailscaleServe, getTailnetHostname } from "../infra/tailscale.js";
//#region src/gateway/server-tailscale.ts
init_logger();
const log = createLogger("GatewayTailscale");
async function startGatewayTailscaleExposure(params) {
	if (params.tailscaleMode === "off") return null;
	if (params.tailscaleMode === "serve") await enableTailscaleServe(params.port);
	else await enableTailscaleFunnel(params.port);
	const host = await getTailnetHostname().catch(() => null);
	if (host) log.info({
		tailscaleMode: params.tailscaleMode,
		host,
		port: params.port
	}, `${params.tailscaleMode} enabled: https://${host}/`);
	else log.info({
		tailscaleMode: params.tailscaleMode,
		port: params.port
	}, `${params.tailscaleMode} enabled`);
	if (!params.resetOnExit) return null;
	return async () => {
		try {
			if (params.tailscaleMode === "serve") await disableTailscaleServe();
			else await disableTailscaleFunnel();
		} catch (err) {
			const em = err instanceof Error ? err.message : String(err);
			log.warn({
				err,
				tailscaleMode: params.tailscaleMode,
				errorMessage: em
			}, `Tailscale ${params.tailscaleMode} cleanup failed: ${em}`);
		}
	};
}
//#endregion
export { startGatewayTailscaleExposure };

//# sourceMappingURL=server-tailscale.js.map