"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablHttpAgent = void 0;
const http_1 = __importDefault(require("http"));
const agent_base_1 = require("agent-base");
const axiosProxyConfig_1 = require("./axiosProxyConfig");
const https = __importStar(require("https"));
const httpUtil_1 = require("./httpUtil");
const http_2 = require("http-cookie-agent/http");
class MablHttpAgent extends agent_base_1.Agent {
    constructor(httpAgent, httpsAgent) {
        super();
        this.httpAgent = httpAgent;
        this.httpsAgent = httpsAgent;
    }
    static async createNewInstance(config) {
        const constructorParameters = await MablHttpAgent.getConstructorParameters(config.httpAgent, config.httpsAgent, config.cookieJar);
        if (config.proxyConfig) {
            const axiosConfig = (0, axiosProxyConfig_1.axiosProxyConfig)(config.proxyConfig);
            if (config.cookieJar) {
                constructorParameters.httpAgent = new http_2.HttpCookieAgent({
                    cookies: { jar: config.cookieJar },
                });
                constructorParameters.httpsAgent = new http_2.HttpsCookieAgent({
                    cookies: { jar: config.cookieJar },
                    rejectUnauthorized: false,
                });
            }
            else {
                if (axiosConfig.httpAgent) {
                    constructorParameters.httpAgent = axiosConfig.httpAgent;
                }
                if (axiosConfig.httpsAgent) {
                    constructorParameters.httpsAgent = axiosConfig.httpsAgent;
                }
            }
        }
        return new MablHttpAgent(constructorParameters.httpAgent, constructorParameters.httpsAgent);
    }
    static async getConstructorParameters(httpAgent, httpsAgent, cookieJar) {
        const proxyConfig = await (0, axiosProxyConfig_1.currentProxyConfig)('test');
        if (cookieJar && !httpAgent && !httpsAgent) {
            if (proxyConfig.httpsAgent) {
                const existingHttpsAgent = proxyConfig.httpsAgent;
                const rejectUnauthorized = existingHttpsAgent.options?.rejectUnauthorized ?? true;
                return {
                    httpAgent: new http_2.HttpCookieAgent({
                        cookies: { jar: cookieJar },
                    }),
                    httpsAgent: new http_2.HttpsCookieAgent({
                        cookies: { jar: cookieJar },
                        rejectUnauthorized,
                    }),
                };
            }
            return {
                httpAgent: new http_2.HttpCookieAgent({
                    cookies: { jar: cookieJar },
                }),
                httpsAgent: new http_2.HttpsCookieAgent({
                    cookies: { jar: cookieJar },
                }),
            };
        }
        return {
            httpAgent: httpAgent ?? proxyConfig.httpAgent ?? http_1.default.globalAgent,
            httpsAgent: httpsAgent ?? proxyConfig.httpsAgent ?? https.globalAgent,
        };
    }
    callback(req, opts) {
        if (this.maybeGetFirstStringHeader(req, httpUtil_1.USER_AGENT_HEADER)?.startsWith('PostmanRuntime/')) {
            req.setHeader(httpUtil_1.USER_AGENT_HEADER, httpUtil_1.USER_AGENT);
        }
        return Promise.resolve(opts.secureEndpoint ? this.httpsAgent : this.httpAgent);
    }
    maybeGetFirstStringHeader(req, name) {
        const value = req.getHeader(name);
        if (Array.isArray(value)) {
            return value[0].toString();
        }
        return value?.toString();
    }
}
exports.MablHttpAgent = MablHttpAgent;
