import { buildLogger } from '../../app/logger.js';
import { isBrowser } from '../../utils/runtime.js';
export function augmentPreprocessRequestWithForwardedFor(options) {
    const originalPreprocessRequest = options.preprocessRequest;
    return async (request, clientOrigin, metadata) => {
        if (!isBrowser()) {
            const headers = new Headers(request.headers);
            const forwardedFor = options.navigatorContextProvider?.()?.forwardedFor;
            if (forwardedFor) {
                headers.set('x-forwarded-for', forwardedFor);
            }
            else {
                const logger = buildLogger(options.loggerOptions);
                logger.warn("[WARNING] Unable to set x-forwarded-for header. Make sure to set the 'forwardedFor' property in the navigator context provider.");
            }
            request.headers = headers;
        }
        if (originalPreprocessRequest) {
            return originalPreprocessRequest(request, clientOrigin, metadata);
        }
        return request;
    };
}
