import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
//#region src/gateway/startup-readiness.ts
init_logger();
const log = createLogger("GatewayStartup");
const STARTUP_UNAVAILABLE_GATEWAY_METHODS = [
	"sessions.history",
	"sessions.messages",
	"sessions.list",
	"sessions.run",
	"sessions.reset",
	"models.list"
];
const DEFAULT_RETRY_AFTER_MS = 500;
const MAX_RETRY_AFTER_MS = 5e3;
var GatewayReadiness = class {
	ready = false;
	httpListening = false;
	startedAtMs = Date.now();
	readyAtMs = null;
	markStarting(startedAtMs = Date.now()) {
		this.startedAtMs = startedAtMs;
		this.readyAtMs = null;
		this.ready = false;
		this.httpListening = false;
	}
	markHttpListening() {
		this.httpListening = true;
	}
	markReady() {
		if (this.ready) return;
		this.ready = true;
		this.readyAtMs = Date.now();
		const durationMs = this.readyAtMs - this.startedAtMs;
		log.info({
			phase: "gateway.startup",
			ready: true,
			startupDurationMs: Math.round(durationMs)
		}, `Gateway ready in ${(durationMs / 1e3).toFixed(1)}s`);
	}
	isReady() {
		return this.ready;
	}
	isHttpListening() {
		return this.httpListening;
	}
	getSnapshot() {
		return {
			ready: this.ready,
			httpListening: this.httpListening,
			startedAtMs: this.startedAtMs,
			readyAtMs: this.readyAtMs,
			startupDurationMs: this.readyAtMs === null ? null : Math.max(0, this.readyAtMs - this.startedAtMs)
		};
	}
	resolveRetryAfterMs(headerValue) {
		if (headerValue) {
			const parsed = Number.parseInt(headerValue, 10);
			if (Number.isFinite(parsed) && parsed > 0) return Math.min(parsed, MAX_RETRY_AFTER_MS);
		}
		return DEFAULT_RETRY_AFTER_MS;
	}
};
function buildStartupUnavailablePayload(params) {
	const retryAfterMs = Math.min(Math.max(params.retryAfterMs ?? DEFAULT_RETRY_AFTER_MS, 100), MAX_RETRY_AFTER_MS);
	return {
		ok: false,
		error: `${params.method} unavailable during gateway startup`,
		code: "STARTUP_UNAVAILABLE",
		retryable: true,
		retryAfterMs,
		method: params.method
	};
}
function parseStartupRetryAfterMs(value) {
	if (typeof value === "number" && Number.isFinite(value)) return Math.min(Math.max(value, 100), MAX_RETRY_AFTER_MS);
	return DEFAULT_RETRY_AFTER_MS;
}
//#endregion
export { GatewayReadiness, STARTUP_UNAVAILABLE_GATEWAY_METHODS, buildStartupUnavailablePayload, parseStartupRetryAfterMs };

//# sourceMappingURL=startup-readiness.js.map