import { HttpProxyAgent, HttpsProxyAgent } from "hpagent";
/// Singleton instance of the proxy configuration.
/// undefined = Config was not yet tried to load
/// null = no config is present
/// ProxyConfig = at least one variable of http_proxy or https_proxy is set
let proxyConfigInstance = undefined;
export function loadProxyConfiguration(logger) {
    if (proxyConfigInstance !== undefined) {
        return proxyConfigInstance;
    }
    let proxyConfig = {
        http_proxy: process.env.http_proxy,
        https_proxy: process.env.https_proxy,
    };
    if (!proxyConfig.http_proxy && !proxyConfig.https_proxy) {
        logger.info("No proxy configuration found.");
        proxyConfigInstance = null;
        return proxyConfigInstance;
    }
    let foundProxyConfig = [
        proxyConfig.https_proxy ? "https_proxy" : "",
        proxyConfig.http_proxy ? "http_proxy" : "",
    ];
    logger.info(`Proxy configuration found for ${foundProxyConfig}`);
    proxyConfigInstance = proxyConfig;
    return proxyConfigInstance;
}
export function getChromiumProxyConfiguration(logger) {
    loadProxyConfiguration(logger);
    if (proxyConfigInstance == null) {
        return null;
    }
    let chromiumProxyConfiguration = "--proxy-server=";
    if (proxyConfigInstance.http_proxy) {
        chromiumProxyConfiguration += `http=${proxyConfigInstance.http_proxy};`;
    }
    if (proxyConfigInstance.https_proxy) {
        chromiumProxyConfiguration += `https=${proxyConfigInstance.https_proxy};`;
    }
    chromiumProxyConfiguration = chromiumProxyConfiguration.slice(0, -1);
    logger.info(`Chromium Proxy Configuration found. ${chromiumProxyConfiguration}`);
    return chromiumProxyConfiguration;
}
export function getGotProxyConfiguration(logger) {
    loadProxyConfiguration(logger);
    if (proxyConfigInstance == null) {
        return null;
    }
    const httpAgent = proxyConfigInstance.http_proxy
        ? new HttpProxyAgent({ proxy: proxyConfigInstance.http_proxy })
        : undefined;
    const httpsAgent = proxyConfigInstance.https_proxy
        ? new HttpsProxyAgent({ proxy: proxyConfigInstance.https_proxy })
        : undefined;
    if (!httpAgent && !httpsAgent) {
        return undefined;
    }
    return {
        ...(httpAgent ? { http: httpAgent } : { http: undefined }),
        ...(httpsAgent ? { https: httpsAgent } : { https: undefined }),
    };
}
