import { g as resolveStateDir } from "./paths-VdmjGGyc.js";
import { t as formatCliCommand } from "./command-format-DOYW_19_.js";
import path from "node:path";
import fs from "node:fs/promises";

//#region src/infra/restart-sentinel.ts
const SENTINEL_FILENAME = "restart-sentinel.json";
function formatDoctorNonInteractiveHint(env = process.env) {
	return `Run: ${formatCliCommand("skykoi doctor --non-interactive", env)}`;
}
function resolveRestartSentinelPath(env = process.env) {
	return path.join(resolveStateDir(env), SENTINEL_FILENAME);
}
async function writeRestartSentinel(payload, env = process.env) {
	const filePath = resolveRestartSentinelPath(env);
	await fs.mkdir(path.dirname(filePath), { recursive: true });
	const data = {
		version: 1,
		payload
	};
	await fs.writeFile(filePath, `${JSON.stringify(data, null, 2)}\n`, "utf-8");
	return filePath;
}
async function readRestartSentinel(env = process.env) {
	const filePath = resolveRestartSentinelPath(env);
	try {
		const raw = await fs.readFile(filePath, "utf-8");
		let parsed;
		try {
			parsed = JSON.parse(raw);
		} catch {
			await fs.unlink(filePath).catch(() => {});
			return null;
		}
		if (!parsed || parsed.version !== 1 || !parsed.payload) {
			await fs.unlink(filePath).catch(() => {});
			return null;
		}
		return parsed;
	} catch {
		return null;
	}
}
async function consumeRestartSentinel(env = process.env) {
	const filePath = resolveRestartSentinelPath(env);
	const parsed = await readRestartSentinel(env);
	if (!parsed) return null;
	await fs.unlink(filePath).catch(() => {});
	return parsed;
}
function formatRestartSentinelMessage(payload) {
	return `GatewayRestart:\n${JSON.stringify(payload, null, 2)}`;
}
function summarizeRestartSentinel(payload) {
	return `Gateway restart ${payload.kind} ${payload.status}${payload.stats?.mode ? ` (${payload.stats.mode})` : ""}`.trim();
}
function trimLogTail(input, maxChars = 8e3) {
	if (!input) return null;
	const text = input.trimEnd();
	if (text.length <= maxChars) return text;
	return `…${text.slice(text.length - maxChars)}`;
}

//#endregion
export { summarizeRestartSentinel as a, readRestartSentinel as i, formatDoctorNonInteractiveHint as n, trimLogTail as o, formatRestartSentinelMessage as r, writeRestartSentinel as s, consumeRestartSentinel as t };