import { logger } from "../util/logger.js";
//#region extensions/weixin/src/api/session-guard.ts
const SESSION_PAUSE_DURATION_MS = 3600 * 1e3;
/** Error code returned by the server when the bot session has expired. */
const SESSION_EXPIRED_ERRCODE = -14;
const pauseUntilMap = /* @__PURE__ */ new Map();
/** Pause all inbound/outbound API calls for `accountId` for one hour. */
function pauseSession(accountId) {
	const until = Date.now() + SESSION_PAUSE_DURATION_MS;
	pauseUntilMap.set(accountId, until);
	logger.info(`session-guard: paused accountId=${accountId} until=${new Date(until).toISOString()} (${SESSION_PAUSE_DURATION_MS / 1e3}s)`);
}
/** Returns `true` when the bot is still within its one-hour cooldown window. */
function isSessionPaused(accountId) {
	const until = pauseUntilMap.get(accountId);
	if (until === void 0) return false;
	if (Date.now() >= until) {
		pauseUntilMap.delete(accountId);
		return false;
	}
	return true;
}
/** Milliseconds remaining until the pause expires (0 when not paused). */
function getRemainingPauseMs(accountId) {
	const until = pauseUntilMap.get(accountId);
	if (until === void 0) return 0;
	const remaining = until - Date.now();
	if (remaining <= 0) {
		pauseUntilMap.delete(accountId);
		return 0;
	}
	return remaining;
}
/** Throw if the session is currently paused. Call before any API request. */
function assertSessionActive(accountId) {
	if (isSessionPaused(accountId)) {
		const remainingMin = Math.ceil(getRemainingPauseMs(accountId) / 6e4);
		throw new Error(`session paused for accountId=${accountId}, ${remainingMin} min remaining (errcode -14)`);
	}
}
/**
* Reset internal state — only for tests.
* @internal
*/
function _resetForTest() {
	pauseUntilMap.clear();
}
//#endregion
export { SESSION_EXPIRED_ERRCODE, _resetForTest, assertSessionActive, getRemainingPauseMs, isSessionPaused, pauseSession };

//# sourceMappingURL=session-guard.js.map