import { resolveStateDir } from "../config/paths-state.js";
import { init_paths } from "../config/paths.js";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
//#region src/tunnel/tunnel-state.ts
init_paths();
const TUNNEL_STATE_FILE = "tunnel.json";
function resolveTunnelStatePath() {
	return join(resolveStateDir(), TUNNEL_STATE_FILE);
}
function loadTunnelState() {
	const path = resolveTunnelStatePath();
	if (!existsSync(path)) return null;
	try {
		const raw = readFileSync(path, "utf8");
		const parsed = JSON.parse(raw);
		if (!parsed.tunnelId || !parsed.tunnelToken) return null;
		return parsed;
	} catch {
		return null;
	}
}
function saveTunnelState(state) {
	const path = resolveTunnelStatePath();
	mkdirSync(resolveStateDir(), { recursive: true });
	writeFileSync(path, `${JSON.stringify(state, null, 2)}\n`, "utf8");
}
function updateTunnelState(patch) {
	const current = loadTunnelState();
	if (!current) return null;
	const next = {
		...current,
		...patch
	};
	saveTunnelState(next);
	return next;
}
function clearTunnelState() {
	const path = resolveTunnelStatePath();
	if (existsSync(path)) writeFileSync(path, "{}\n", "utf8");
}
//#endregion
export { clearTunnelState, loadTunnelState, resolveTunnelStatePath, saveTunnelState, updateTunnelState };

//# sourceMappingURL=tunnel-state.js.map