import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
//#region src/tunnel/broker-client.ts
init_logger();
const log = createLogger("TunnelBroker");
var TunnelBrokerClient = class {
	constructor(baseUrl) {
		this.baseUrl = baseUrl;
	}
	apiUrl(path) {
		return `${this.baseUrl.replace(/\/+$/, "")}${path.startsWith("/") ? path : `/${path}`}`;
	}
	async register(input) {
		const url = this.apiUrl("/tunnels/register");
		const res = await fetch(url, {
			method: "POST",
			headers: {
				"Content-Type": "application/json",
				"X-Registration-Secret": input.registrationSecret
			},
			body: JSON.stringify({
				gatewayVersion: input.gatewayVersion,
				platform: input.platform,
				gatewayTokenHash: input.gatewayTokenHash,
				preferredSubdomain: input.preferredSubdomain
			})
		});
		if (!res.ok) {
			const body = await res.text().catch(() => "");
			log.error({
				status: res.status,
				url,
				bodyPreview: body.slice(0, 200)
			}, "Tunnel register failed");
			throw new Error(`Tunnel register failed: ${res.status} ${res.statusText}`);
		}
		return await res.json();
	}
	async heartbeat(tunnelId, tunnelToken) {
		const res = await fetch(this.apiUrl(`/tunnels/${encodeURIComponent(tunnelId)}/heartbeat`), {
			method: "POST",
			headers: { "X-Tunnel-Token": tunnelToken }
		});
		if (res.status === 401 || res.status === 410) throw new Error(`Tunnel heartbeat rejected: ${res.status}`);
		if (!res.ok) throw new Error(`Tunnel heartbeat failed: ${res.status}`);
	}
	async deregister(tunnelId, tunnelToken) {
		const res = await fetch(this.apiUrl(`/tunnels/${encodeURIComponent(tunnelId)}`), {
			method: "DELETE",
			headers: { "X-Tunnel-Token": tunnelToken }
		});
		if (!res.ok && res.status !== 404) log.warn({
			tunnelId,
			status: res.status
		}, "Tunnel deregister returned non-OK");
	}
	async setDnsChallenge(input) {
		const res = await fetch(this.apiUrl("/dns/challenge"), {
			method: "POST",
			headers: {
				"Content-Type": "application/json",
				"X-Tunnel-Token": input.tunnelToken
			},
			body: JSON.stringify({
				tunnelId: input.tunnelId,
				subdomain: input.subdomain,
				txtValue: input.txtValue
			})
		});
		if (!res.ok) {
			const body = await res.text().catch(() => "");
			throw new Error(`Broker DNS challenge failed: ${res.status} ${body.slice(0, 200)}`);
		}
		return await res.json();
	}
	async cleanupDnsChallenge(input) {
		const res = await fetch(this.apiUrl("/dns/cleanup"), {
			method: "POST",
			headers: {
				"Content-Type": "application/json",
				"X-Tunnel-Token": input.tunnelToken
			},
			body: JSON.stringify({
				tunnelId: input.tunnelId,
				recordId: input.recordId
			})
		});
		if (!res.ok && res.status !== 404) log.warn({
			recordId: input.recordId,
			status: res.status
		}, "Broker DNS cleanup returned non-OK");
	}
};
function resolveBrokerApiBase(brokerUrl) {
	const trimmed = brokerUrl.replace(/\/+$/, "");
	if (trimmed.endsWith("/api")) return trimmed;
	return `${trimmed}/api`;
}
//#endregion
export { TunnelBrokerClient, resolveBrokerApiBase };

//# sourceMappingURL=broker-client.js.map