/**
 * Minimal AsyncLocalStorage context for request-scoped data.
 * Currently used only to propagate accountability into createAuditLog
 * so audit entries capture userId/IP even when ItemsService has no explicit accountability.
 */
import { AsyncLocalStorage } from 'node:async_hooks';
/** Singleton ALS instance */
export const requestContext = new AsyncLocalStorage();
/** Get the current request's accountability (or undefined outside a request) */
export function getContextAccountability() {
    return requestContext.getStore()?.accountability;
}
/**
 * Get the request-scoped $CURRENT_USER resolution cache, creating it lazily.
 * Returns undefined when there is no active request store (e.g. workflows, tasks,
 * scheduled hooks) — callers should resolve directly in that case.
 */
export function getUserResolveCache() {
    const store = requestContext.getStore();
    if (!store)
        return undefined;
    if (!store.userResolveCache) {
        store.userResolveCache = new Map();
    }
    return store.userResolveCache;
}
/** Set accountability on the current request store */
export function setContextAccountability(accountability) {
    const store = requestContext.getStore();
    if (store) {
        store.accountability = accountability;
    }
}
/**
 * Express middleware — wraps the rest of the request in an ALS context.
 * Must be mounted BEFORE authMiddleware so the store exists when auth writes to it.
 */
export function requestContextMiddleware(req, _res, next) {
    requestContext.run({}, () => next());
}
//# sourceMappingURL=requestContext.js.map