import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { isTailscaleInstalled } from "../infra/tailscale.js";
import { resolveGatewayBindMode } from "../config/gateway-bind.js";
import { getTunnelConsentState } from "../tunnel/consent.js";
import { getTunnelService } from "../tunnel/tunnel-service.js";
import { maybeAutoStartTunnelFromConfig } from "../tunnel/gateway-lifecycle.js";
import { collectExposureConflicts } from "./exposure-guards.js";
import { getTailscaleExposureState, maybeStartTailscaleFromConfig, stopTailscaleExposure } from "../gateway/tailscale-lifecycle.js";
//#region src/remote-access/exposure-manager.ts
init_logger();
const log = createLogger("ExposureManager");
var ExposureManager = class {
	async getStatus(config) {
		const tunnelStatus = getTunnelService().getStatus();
		const consent = getTunnelConsentState(config);
		const ts = getTailscaleExposureState();
		const cliAvailable = await isTailscaleInstalled();
		return {
			bindMode: resolveGatewayBindMode(config),
			tailscale: {
				...ts,
				cliAvailable
			},
			tunnel: {
				...tunnelStatus,
				consentRequired: consent.consentRequired,
				canAutoStart: consent.canAutoStart
			},
			conflicts: collectExposureConflicts(config)
		};
	}
	async autoStart(config, port, gatewayToken) {
		try {
			await maybeStartTailscaleFromConfig(config, port);
		} catch (err) {
			const em = err instanceof Error ? err.message : String(err);
			log.warn({
				err,
				errorMessage: em,
				phase: "tailscale_autostart"
			}, `Tailscale auto-start skipped: ${em}`);
		}
		await maybeAutoStartTunnelFromConfig(config, gatewayToken);
	}
	async startTailscale(config, port, mode) {
		await maybeStartTailscaleFromConfig(config, port, mode);
	}
	async stopTailscale() {
		await stopTailscaleExposure();
	}
};
let singleton = null;
function getExposureManager() {
	if (!singleton) singleton = new ExposureManager();
	return singleton;
}
//#endregion
export { ExposureManager, getExposureManager };

//# sourceMappingURL=exposure-manager.js.map