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";
//#region src/share/share-rate-limit.ts
init_logger();
const log = createLogger("ShareRateLimit");
/**
* Two-level limiter for public share download routes: a short burst window
* and a longer aggregate window. The short window kicks in first; if it
* blocks, the long window isn't consumed (we don't want to penalize twice).
*/
function consumeSharePublicLimit(clientIp) {
	const short = buckets.sharePublicShort().consume(clientIp);
	if (!short.allowed) {
		log.warn({
			clientIp,
			retryAfterSec: Math.ceil(short.retryAfterMs / 1e3),
			reason: "share_short_window"
		}, "Share public rate limit exceeded (short window)");
		return {
			allowed: false,
			retryAfterMs: short.retryAfterMs
		};
	}
	const long = buckets.sharePublicLong().consume(clientIp);
	if (!long.allowed) {
		log.warn({
			clientIp,
			retryAfterSec: Math.ceil(long.retryAfterMs / 1e3),
			reason: "share_long_window"
		}, "Share public rate limit exceeded (long window)");
		return {
			allowed: false,
			retryAfterMs: long.retryAfterMs
		};
	}
	return {
		allowed: true,
		retryAfterMs: 0
	};
}
/** @internal */
function resetSharePublicLimitsForTests() {
	buckets.sharePublicShort().resetForTests();
	buckets.sharePublicLong().resetForTests();
}
//#endregion
export { consumeSharePublicLimit, resetSharePublicLimitsForTests };

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