import { log } from '@mondaydotcomorg/atp-runtime';
import { parseQuery } from '../core/http.js';
/**
 * Creates a request context object with all necessary helpers and providers
 */
export function createContext(options) {
    const { req, cacheProvider, authProvider, auditSink, customLogger, responseHeaders } = options;
    const clientId = req.headers['x-client-id'] || undefined;
    const userId = req.headers['x-user-id'] || undefined;
    const url = req.url || '/';
    const queryIndex = url.indexOf('?');
    const path = queryIndex === -1 ? url : url.substring(0, queryIndex);
    return {
        method: req.method || 'GET',
        path,
        query: parseQuery(url),
        headers: req.headers,
        body: null,
        status: 200,
        responseBody: null,
        clientId,
        userId,
        cache: cacheProvider,
        auth: authProvider,
        audit: auditSink,
        logger: customLogger || log,
        throw: (status, message) => {
            const error = new Error(message);
            error.status = status;
            throw error;
        },
        assert: (condition, message) => {
            if (!condition) {
                const error = new Error(message);
                error.status = 400;
                throw error;
            }
        },
        set: (header, value) => {
            if (!responseHeaders.has(req)) {
                responseHeaders.set(req, new Map());
            }
            responseHeaders.get(req).set(header, value);
        },
    };
}
//# sourceMappingURL=context.js.map