import { parse as urlToHttpOptions } from 'url'; // should be replaced with `urlToHttpOptions` after supporting node >=16
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { lookupWithCache } from './dns-cache.js';
import createHttpProxyAgent from 'http-proxy-agent';
import createHttpsProxyAgent from 'https-proxy-agent';
import * as utils from '@applitools/utils';
export function makeAgent({ proxy, useDnsCache, keepAliveOptions, }) {
    return function agent(url) {
        var _a, _b;
        const proxyOptions = utils.types.isFunction(proxy) ? proxy(url) : proxy;
        const lookup = useDnsCache ? lookupWithCache : undefined;
        if (proxyOptions) {
            const proxyUrl = new URL(proxyOptions.url);
            proxyUrl.username = (_a = proxyOptions.username) !== null && _a !== void 0 ? _a : proxyUrl.username;
            proxyUrl.password = (_b = proxyOptions.password) !== null && _b !== void 0 ? _b : proxyUrl.password;
            const options = { ...urlToHttpOptions(proxyUrl.href), rejectUnauthorized: false, lookup };
            if (url.protocol === 'https:') {
                const agent = createHttpsProxyAgent(options);
                agent.callback = utils.general.wrap(agent.callback.bind(agent), (fn, request, options, ...rest) => {
                    return fn(request, { ...options, rejectUnauthorized: false }, ...rest);
                });
                return agent;
            }
            else if (url.protocol === 'http:') {
                return createHttpProxyAgent(options);
            }
        }
        else if (url.protocol === 'https:') {
            return new HttpsAgent({ rejectUnauthorized: false, lookup, ...keepAliveOptions });
        }
        else if (url.protocol === 'http:') {
            // @ts-expect-error due to a wrong type definition for node 12, already fixed in newer versions
            return new HttpAgent({ lookup, ...keepAliveOptions });
        }
    };
}
