"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.enrichContextWithIp = exports.createContext = void 0;
// Copy of https://github.com/Unleash/unleash-proxy/blob/main/src/create-context.ts.
const crypto_1 = __importDefault(require("crypto"));
function createContext(value) {
    const { appName, environment, userId, sessionId, remoteAddress, properties, ...rest } = value;
    // move non root context fields to properties
    const context = {
        appName,
        environment,
        userId,
        sessionId,
        remoteAddress,
        properties: Object.assign({}, rest, properties),
    };
    // Clean undefined properties on the context
    const cleanContext = Object.keys(context)
        .filter((k) => context[k])
        .reduce((a, k) => ({ ...a, [k]: context[k] }), {});
    return cleanContext;
}
exports.createContext = createContext;
const enrichContextWithIp = (query, ip) => {
    query.remoteAddress = query.remoteAddress || ip;
    query.sessionId = query.sessionId || crypto_1.default.randomBytes(18).toString('hex');
    return createContext(query);
};
exports.enrichContextWithIp = enrichContextWithIp;
//# sourceMappingURL=create-context.js.map