"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GENERAL_API_STEP_EXECUTION_ERROR_CODE = exports.EXECUTION_ENGINE_SSL_VERIFY = exports.USER_AGENT_HEADER = exports.USER_AGENT = void 0;
exports.postProcessHeaders = postProcessHeaders;
const pureUtil_1 = require("../util/pureUtil");
const UPSTREAM_PROTOCOL_HEADER = 'x-upstream-protocol';
exports.USER_AGENT = `${(0, pureUtil_1.getCliName)()}@${(0, pureUtil_1.getCliVersion)()}`;
exports.USER_AGENT_HEADER = 'User-Agent';
exports.EXECUTION_ENGINE_SSL_VERIFY = false;
exports.GENERAL_API_STEP_EXECUTION_ERROR_CODE = '-1';
const HEADERS_WITH_COMMA_VALUES = new Set([
    'date',
    'expires',
    'last-modified',
    'if-modified-since',
    'if-unmodified-since',
    'retry-after',
]);
function postProcessHeaders(response) {
    const parsed = {};
    const rawHeaders = response.request?.res?.rawHeaders;
    if (!rawHeaders) {
        return parsed;
    }
    let upstreamWasHttp2 = false;
    for (let i = 0; i + 1 < rawHeaders.length; i += 2) {
        if (rawHeaders[i].toLowerCase() === UPSTREAM_PROTOCOL_HEADER) {
            upstreamWasHttp2 = rawHeaders[i + 1] === 'h2';
            break;
        }
    }
    for (let i = 0; i + 1 < rawHeaders.length; i += 2) {
        const headerName = rawHeaders[i].toLowerCase();
        const headerValue = rawHeaders[i + 1];
        if (headerName === UPSTREAM_PROTOCOL_HEADER) {
            continue;
        }
        let values;
        if (upstreamWasHttp2 && !HEADERS_WITH_COMMA_VALUES.has(headerName)) {
            values = headerValue.split(', ');
        }
        else {
            values = [headerValue];
        }
        parsed[headerName] = (parsed[headerName] ?? []).concat(values);
    }
    return parsed;
}
