import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { buckets } from "../gateway/rate-limit/buckets.js";
import "../gateway/rate-limit/index.js";
import { createHash } from "node:crypto";
//#region src/tunnel/tunnel-rate-limit.ts
init_logger();
const log = createLogger("TunnelRateLimit");
function tokenKey(token) {
	return createHash("sha256").update(token, "utf8").digest("hex").slice(0, 16);
}
function consumeTunnelMutationLimit(token) {
	const result = buckets.tunnelMutate().consume(tokenKey(token));
	if (!result.allowed) log.warn({
		tokenPrefix: token.slice(0, 8),
		retryAfterSec: Math.ceil(result.retryAfterMs / 1e3),
		reason: "tunnel_mutation_limit"
	}, "Tunnel mutation rate limit exceeded");
	return {
		allowed: result.allowed,
		retryAfterMs: result.retryAfterMs
	};
}
/** @internal */
function resetTunnelMutationLimitsForTests() {
	buckets.tunnelMutate().resetForTests();
}
//#endregion
export { consumeTunnelMutationLimit, resetTunnelMutationLimitsForTests };

//# sourceMappingURL=tunnel-rate-limit.js.map